0

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

这是从资产文件夹获取可绘制的代码:

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)
4

2 回答 2

1

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

于 2012-08-18T11:27:41.650 回答
0

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

这消耗更少的内存:

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

view.setImageDrawable( Drawable.createFromPath( filename ) );
于 2015-10-24T22:44:31.407 回答