Mansi
Mansi

Reputation: 1949

android - out of memory exception when get drawable from assets

In my android projects there is custom Grid View with images. Images are comes from assets folder.

Here it is code for get drawable from assets folder:

Drawable imgDrawable = Drawable.createFromStream(myContext.getAssets().open(imgPath),null);

after scrolling grid view its give out of memory exception Log Displays:

01-10 05:24:32.492: E/AndroidRuntime(2303): FATAL EXCEPTION: main
01-10 05:24:32.492: E/AndroidRuntime(2303): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
01-10 05:24:32.492: E/AndroidRuntime(2303):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
01-10 05:24:32.492: E/AndroidRuntime(2303):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:460)
01-10 05:24:32.492: E/AndroidRuntime(2303):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:336)
01-10 05:24:32.492: E/AndroidRuntime(2303):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
01-10 05:24:32.492: E/AndroidRuntime(2303):     at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)
01-10 05:24:32.492: E/AndroidRuntime(2303):     at org.Infoware.BookShelf.BookShelfActivity$BookshelfAdapter.getView(BookShelfActivity.java:331)
01-10 05:24:32.492: E/AndroidRuntime(2303):     at android.widget.AbsListView.obtainView(AbsListView.java:1439)
01-10 05:24:32.492: E/AndroidRuntime(2303):     at android.widget.GridView.makeAndAddView(GridView.java:1222)
01-10 05:24:32.492: E/AndroidRuntime(2303):     at android.widget.GridView.makeRow(GridView.java:268)
01-10 05:24:32.492: E/AndroidRuntime(2303):     at android.widget.GridView.fillDown(GridView.java:221)
01-10 05:24:32.492: E/AndroidRuntime(2303):     at android.widget.GridView.fillGap(GridView.java:188)
01-10 05:24:32.492: E/AndroidRuntime(2303):     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:3569)
01-10 05:24:32.492: E/AndroidRuntime(2303):     at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:3067)
01-10 05:24:32.492: E/AndroidRuntime(2303):     at android.os.Handler.handleCallback(Handler.java:587)
01-10 05:24:32.492: E/AndroidRuntime(2303):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-10 05:24:32.492: E/AndroidRuntime(2303):     at android.os.Looper.loop(Looper.java:130)
01-10 05:24:32.492: E/AndroidRuntime(2303):     at android.app.ActivityThread.main(ActivityThread.java:3683)
01-10 05:24:32.492: E/AndroidRuntime(2303):     at java.lang.reflect.Method.invokeNative(Native Method)
01-10 05:24:32.492: E/AndroidRuntime(2303):     at java.lang.reflect.Method.invoke(Method.java:507)
01-10 05:24:32.492: E/AndroidRuntime(2303):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:850)
01-10 05:24:32.492: E/AndroidRuntime(2303):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
01-10 05:24:32.492: E/AndroidRuntime(2303):     at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 1676

Answers (2)

Ignacio Roda
Ignacio Roda

Reputation: 151

I was having the same issue. Then I decided to try using Bitmaps instead of Drawables and I'm not getting the OutOfMemoryException anymore.

This is consuming less memory:

view.setImageBitmap( BitmapFactory.decodeFile( filename ) );

than

view.setImageDrawable( Drawable.createFromPath( filename ) );

Upvotes: 1

Yusuf X
Yusuf X

Reputation: 14633

In my experience, this happens usually because you have a memory leak somewhere else, and then your app could croak anywhere but is most likely to run out of memory during a bitmap operation.

Upvotes: 1

Related Questions