MISTAKE:
2019-07-25 23:35:33.030 13258-13258/com.example.images E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.images, PID: 13258 java.lang.OutOfMemoryError: Failed to allocate a 1179136012 byte allocation with 4194304 free bytes and 350MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:620) at android .graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:455) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1155) at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:720) at android. content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:571) at android.content.res.Resources.loadDrawable(Resources.java:858) at android.content.res.TypedArray.getDrawable(TypedArray.java:928) at android.widget.ImageView.(ImageView.java:162) at android.widget.ImageView.(ImageView.java:150) at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:71) at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:67) at android.support.v7.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:181) at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:105) at android.support.v7.app.AppCompatDelegateImplV9.createView (AppCompatDelegateImplV9.java:1035) at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1092)
Out of memory, I have tried to put a thousand solutions as in Manifest
the android:largeHeap="true"
typical one and another that is usually commented on but nothing, if I put icons instead of large images, it works, because of the size, how can I incorporate images of 10 MB or more in my App or does it have a limit and I have to download them to 5MB or how much?
Landscape.java:
public class Landscapes extends AppCompatActivity {
RecyclerView mRecyclerView;
int[] mPlaceList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_landscapes);
mRecyclerView = findViewById(R.id.recyclerview);
GridLayoutManager mGridLayoutManager = new GridLayoutManager(Landscapes.this, 2);
mRecyclerView.setLayoutManager(mGridLayoutManager);
mPlaceList = new int[]{R.drawable.calendar, R.drawable.coliseum, R.drawable.park,
R.drawable.salad};
MyAdapter myAdapter = new MyAdapter(Landscapes.this, mPlaceList);
mRecyclerView.setAdapter(myAdapter);
}
}
activity_landscape.xml:
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
Adapter.java:
public class MyAdapter extends RecyclerView.Adapter<PlaceViewHolder> {
private Context mContext;
private int[] mPlaceList;
public MyAdapter(Context mContext, int[] mPlaceList) {
this.mContext = mContext;
this.mPlaceList = mPlaceList;
}
@Override
public PlaceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_custom_layout,
parent, false);
return new PlaceViewHolder(view);
}
@Override
public void onBindViewHolder(final PlaceViewHolder holder, int position) {
holder.mPlace.setImageResource(mPlaceList[position]);
holder.mPlace.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent mIntent = new Intent(mContext, DetailActivity.class);
mIntent.putExtra("Image", mPlaceList[holder.getAdapterPosition()]);
mContext.startActivity(mIntent);
}
});
}
@Override
public int getItemCount() {
return mPlaceList.length;
}
}
class PlaceViewHolder extends RecyclerView.ViewHolder {
ImageView mPlace;
public PlaceViewHolder(View itemView) {
super(itemView);
mPlace = itemView.findViewById(R.id.ivPlace);
}
}
activity_Detail.xml :
I only have the imageView with id: imageView
Detail.activity.java :
public class DetailActivity extends AppCompatActivity {
ImageView mPlace;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
mPlace = findViewById(R.id.imageView);
Bundle mBundle = getIntent().getExtras();
if(mBundle != null){
mPlace.setImageResource(mBundle.getInt("Image"));
}
}
}
Update bug:
2019-07-26 18:07:59.533 16235-16235/com.example.images E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.images, PID: 16235 java.lang.OutOfMemoryError: Failed to allocate a 1179136012 byte allocation with 4194304 free bytes and 343MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:620) at android .graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:455) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1155) at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:720) at android. content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:571) at android.content.res.Resources.getDrawable(Resources.java:771) at android.content.Context.getDrawable(Context.java:525) at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java :358) at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:198) at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186) at android.support.v7.content. res.AppCompatResources.getDrawable(AppCompatResources.java:100) at android.support.v7.widget.AppCompatImageHelper.setImageResource(AppCompatImageHelper.java:85) at android.support.v7.widget.AppCompatImageView.setImageResource(AppCompatImageView.java:93) at com.example.images.DetailActivity.onCreate(DetailActivity.java:19) at android.app.Activity.performCreate(Activity.java:6679) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) at android. app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler. dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Hello Rafa is a problem indicated here:
Your directory images
/Drawable
which are :They have a considerable size and cannot be allocated in memory, consider optimizing them.
You can review the information in this question:
Good image resolution causes "OutOfMemoryError"
as an option you can use
GLIDE
orPicasso
, here you can find examples:Imageview download image with Glide or Picasso
With Glide
Glide
, you can load images defined in resources for example an image calledandroid.jpg
inside/drawable
:adding the dependency inside your build.gradle file:
Picasso : With
Picasso
you can load images defined in the resources for example an image calledandroid.jpg
inside/drawable
:adding the respective dependency inside your file
build.gradle
:With both options you would have the same result:
Update:
To implement
Glide
, you must do it in your Adapter.You can also use
Glide
when loading the image in the Activity