gpt4 book ai didi

android - 将背景图像添加到 TableView 中的单元格时出现 OutOfMemoryError

转载 作者:行者123 更新时间:2023-11-30 03:04:29 27 4
gpt4 key购买 nike

我在我的 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)

最佳答案

我最终通过将位图相关代码移至异步任务来修复此问题

    @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);
}
}

关于android - 将背景图像添加到 TableView 中的单元格时出现 OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22027674/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com