Asaf
Asaf

Reputation: 31

Failed to allocate memory from loading jpg files - Android studio Erro

I have a function in my app that adds offline pictures that are stored inside the app's memory to a listview. The code is extremely simple as you can see:

if(FirstTag != null) {
        if (FirstTag.equals("care"))
            imageView.setImageDrawable(context.getApplicationContext().getResources().getDrawable(R.drawable.care));
        else if (FirstTag.equals("holidays"))
            imageView.setImageDrawable(context.getApplicationContext().getResources().getDrawable(R.drawable.holidays));
        else if (FirstTag.equals("Thing Different"))
            imageView.setImageDrawable(context.getApplicationContext().getResources().getDrawable(R.drawable.thinkdifferent2));
        else if (FirstTag.equals("Fun Activities"))
            imageView.setImageDrawable(context.getApplicationContext().getResources().getDrawable(R.drawable.fun));
        else if (FirstTag.equals("מערך חברות")) }

it simply checks a value in a json object and put a picture in the listview by this value, of course there are 20 values and not 5, but I didn't think I should Include all... All the pictures are compressed, and when I don't load many of them the app doesn't crash. But when I try to launch the app with a picture per each item in the listview, everything seems to work perfectly fine, and then when you scroll down the app lags and in a second it crashes with the following error:

9-30 18:35:18.299 7859-7859/com.example.negev.peulibraryv201 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.negev.peulibraryv201, PID: 7859 java.lang.OutOfMemoryError: Failed to allocate a 681099532 byte allocation with 16777216 free bytes and 261MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:700) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:535) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1179) at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:770) at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:621) at android.content.res.Resources.getDrawable(Resources.java:1640) at android.content.res.Resources.getDrawable(Resources.java:1611) at com.example.negev.peulibraryv201.GamesLibraryAdapter.getView(GamesLibraryAdapter.java:70) at android.widget.AbsListView.obtainView(AbsListView.java:3238) at android.widget.ListView.makeAndAddView(ListView.java:2147) at android.widget.ListView.fillDown(ListView.java:767) at android.widget.ListView.fillGap(ListView.java:731) at android.widget.AbsListView.trackMotionScroll(AbsListView.java:8292) at android.widget.ListView.trackMotionScroll(ListView.java:2065) at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:7719) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:927) at android.view.Choreographer.doCallbacks(Choreographer.java:702) at android.view.Choreographer.doFrame(Choreographer.java:635) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:913) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6682) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

I already added those line to my manifest:

android:hardwareAccelerated="false"
    android:largeHeap="true"

and it didn't work, wht do you think I can do? Thank you!

Upvotes: 0

Views: 242

Answers (1)

Itamar Kerbel
Itamar Kerbel

Reputation: 2568

What you should do is load the data dynamically as it becomes visible on the screen. Since this is hard work Google already did it for you. You need to use an ArrayAdapter with the list view and let Andriod ask you for more data when it is needed. Its too much code to explain in detail but have a look at this Goolge I/O video. There are many tutorials on the web that explains how to do that in detail. Have a look here.

Upvotes: 0

Related Questions