android - 从资产中获取drawable时的内存不足异常

时间:2012-08-18 11:24:17

标签: android image out-of-memory

在我的android项目中,有自定义网格视图和图像。图像来自资产文件夹。

这是从资产文件夹获取drawable的代码:

Drawable imgDrawable = Drawable.createFromStream(myContext.getAssets().open(imgPath),null);
滚动网格视图后,它会给出内存不足异常 日志显示:

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)

2 个答案:

答案 0 :(得分:1)

根据我的经验,这通常是因为你在其他地方有内存泄漏,然后你的应用程序可能会在任何地方呱呱叫,但在位图操作期间最有可能耗尽内存。

答案 1 :(得分:0)

我遇到了同样的问题。然后我决定尝试使用Bitmaps而不是Drawables,我不再得到OutOfMemoryException了。

这消耗更少的内存:

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

大于

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