在表视图中将backgroundimage添加到单元格时出现OutOfMemoryError

时间:2014-02-25 22:31:28

标签: android out-of-memory

我在Android应用程序中收到一个OutOfMemoryExeption,谷歌搜索了几个小时之后就找不到了。希望有人可以帮助我。

我有一个listview,在这个函数中:

public View getView(int position, View convertView, ViewGroup parent) {
...

    Bitmap image = getBitmapFromMemCache(imageFilePath);
    if (image == null) {
        addBitmapToMemoryCache(imageFilePath, BitmapFactory.decodeFile(imageFilePath));
        image = getBitmapFromMemCache(imageFilePath);
    } 

    if (((WallActivity) context).isFavorite(position)) {
        Drawable drawable = new BitmapDrawable(context.getResources(), image);
        if (android.os.Build.VERSION.SDK_INT < 16) {
            convertView.setBackgroundDrawable(drawable);
        } else {
            convertView.setBackground(drawable);
        }
    }
...
}

此行发生错误:

Drawable drawable = new BitmapDrawable(context.getResources(), image);

我不能使用缩减选项,因为我的图像已经缩小了设备存储空间。这些是日志:

java.lang.OutOfMemoryError: (Heap Size=131079KB, Allocated=129644KB)
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:658)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:347)
at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:430)
at com.application.app.model.adapters.WallAdapter.setView(WallAdapter.java:252)
at com.application.app.model.adapters.WallAdapter.getView(WallAdapter.java:211)
at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
at android.widget.AbsListView.obtainView(AbsListView.java:2334)
at android.widget.ListView.makeAndAddView(ListView.java:1937)
at android.widget.ListView.fillDown(ListView.java:789)
at android.widget.ListView.fillGap(ListView.java:753)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5259)
at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4413)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:734)
at android.view.Choreographer.doCallbacks(Choreographer.java:560)
at android.view.Choreographer.doFrame(Choreographer.java:527)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:719)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5454)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

我通过将位图相关代码移动到asynctask

来最终修复此问题
    @Override
    protected Bitmap doInBackground(String... params) {
        imageFilePath = params[0];
        Bitmap image = getBitmapFromMemCache(imageFilePath);
        if (image == null) {
            try {
                addBitmapToMemoryCache(imageFilePath, BitmapFactory.decodeFile(imageFilePath));
                image = getBitmapFromMemCache(imageFilePath);

            } catch (OutOfMemoryError e) {
                e.printStackTrace();
            }
        }
        return image;
    }

    @Override
    protected void onPostExecute(Bitmap bm) {
        if (bm != null) {
            Drawable drawable = new BitmapDrawable(context.getResources(), bm);
            if (android.os.Build.VERSION.SDK_INT < 16) {
                convertView.setBackgroundDrawable(drawable);
            } else {
                convertView.setBackground(drawable);
            }
        } else {
            convertView.setBackgroundColor(Color.BLACK);
        }
    }
相关问题