1

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:

screenshot

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.

Community
  • 1
  • 1
Kevin Mann
  • 721
  • 1
  • 7
  • 14
  • Increase Memory of your Emulator to be sure it's a 'real' outOfMemory :) (I'd test on real devices to see if the problem really occurs). – Laurent Meyer Mar 16 '15 at 17:06
  • possible duplicate of [Android OutOfMemoryError:?](http://stackoverflow.com/questions/21012006/android-outofmemoryerror) – 323go Mar 16 '15 at 17:07
  • Please refer to the many duplicate questions and online tutorials. – 323go Mar 16 '15 at 17:08
  • thanks @323go I understand there are other similar question on here and referred to them in my question. I am talking about using images within a listview and from the drawable directories, and I can't find anything that matches this situation. I am newish to Android and know this will be a common area of frustration for those switching from iOS development, and hoped an answer to this would help others facing this same set up. – Kevin Mann Mar 16 '15 at 17:13
  • @LaurentMeyer is that a safe thing to do? Are the emulators set to be very low vs devices? – Kevin Mann Mar 16 '15 at 17:14
  • 1
    The principles are the same. You have images that are too large, and you need to manage your bitmaps. – 323go Mar 16 '15 at 17:14
  • @323go so I should be running my images through a bitmapfactory, not just setting them from the drawable resources? – Kevin Mann Mar 16 '15 at 17:16
  • 1
    @323go You're right, he needs to manage bitmaps but first, I'd be sure that his emulator is not running with 64MB of RAM because then it's just device problem and not code problem (BTW, nothing replaces testing on real devices). – Laurent Meyer Mar 16 '15 at 17:17
  • @LaurentMeyer The emulator has 1GB Ram and a 64Mb VM Heap size. – Kevin Mann Mar 16 '15 at 17:20
  • 1
    Then, listen to @323go advices and manage memory properly ;) – Laurent Meyer Mar 16 '15 at 17:21
  • Ok thank you both, I will go read some more about image memory management on Android. Like I said in the question I am a little lost, I have been reading about it and trying things for a couple of hours hoped for some pointers as to where to start. The top answer in the posted possible duplicatation was to put android:largeHeap="true" in the manifest but other similar answers in different questions say not to do that. I don't understand the switching to bitmaps, from the drawable, as I thought that drawable folders were the correct way to handle different images for different devices. – Kevin Mann Mar 16 '15 at 17:33

0 Answers0