I am fairly new to Android development and running into a problem with the memory. In terms of mobile I tend to create iOS apps. I have a MainActivity with a list view as shown in this image:
I have 9 sessions, when I have the same image it scrolls smoothly but when I use 9 different images it strutters and when I get towards the bottom of the list I get this OutOfMemoryError error:
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:422)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
at android.content.res.Resources.loadDrawable(Resources.java:2110)
at android.content.res.Resources.getDrawable(Resources.java:700)
at ly.mann.kevinmann.transformingwork.CellAdapter.getView(CellAdapter.java:57)
I have read a few answers on here to similar things but I am a little lost as of what to do next.
The code I am using in the Adapter to display the images is:
ObjSession cellData = mSessions.get(position);
if(cellData != null){
// Get real header image with fall back
String uri = "@drawable/" + cellData.mPreview ;
Drawable res;
int imageResource = mContext.getResources().getIdentifier(uri, null, mContext.getPackageName());
try{
res = mContext.getResources().getDrawable(imageResource);
}
catch(Resources.NotFoundException e) {
imageResource = mContext.getResources().getIdentifier("@drawable/following", null, mContext.getPackageName());
res = mContext.getResources().getDrawable(imageResource);
}
holder.previewImageView.setImageDrawable(res);
holder.titleLabel.setText(cellData.mTitle);
holder.introLabel.setText(cellData.mIntro);
}
Any help would be amazing.
EDIT: I have tried following the possible duplication listed in the comments below - Android OutOfMemoryError:?
the solution to which does not fix my issue. I have also tried making the images smaller, moving the ones I had to MDPI to HDPI and LDPI to MDPI.