gpt4 book ai didi

java - 有效加载位图 - 仍然出现内存不足错误

转载 作者:行者123 更新时间:2023-12-01 09:17:50 25 4
gpt4 key购买 nike

加载自定义图像时出现内存不足错误。我读过https://developer.android.com/training/displaying-bitmaps/load-bitmap.html寻求帮助。

我按照示例对流进行解码以首先获取大小信息,然后进行解码。第一次解码时仍然崩溃。有办法解决这个问题吗?

ava.lang.OutOfMemoryError:无法分配具有 16776928 可用字节和 25MB 的 48771084 字节分配,直到 OOMBackgroundImageManager.java,第 84 行

dalvik.system.VMRuntime.newNonMovableArray native 方法2 android.graphics.BitmapFactory.nativeDecodeStream 原生方法3 android.graphics.BitmapFactory.decodeStreamInternal BitmapFactory.java,第 882 行4 android.graphics.BitmapFactory.decodeStream BitmapFactory.java,第 858 行5 android.graphics.BitmapFactory.decodeStream BitmapFactory.java,第 896 行6 com.myapp.Utils.BackgroundImageManager.backgroundBackgroundImageManager.java,第8行

public class BackgroundImageManager {
private final static String TAG = BackgroundImageManager.class.getSimpleName();
private static InputStream currentBackgroundImage;

public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

final int halfHeight = height / 2;
final int halfWidth = width / 2;

// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
Log.v("Biscuit-Sample", String.valueOf(inSampleSize));
if (inSampleSize < 4) {
inSampleSize = 4;
}
return inSampleSize;
}

public static Drawable background(Context context, Store store) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

String bgUri = null;
int bgId = 0;
if (store != null) {
bgUri = store.backgroundImageURI;
bgId = store.backgroundImageNumber;
}

if (currentBackgroundImage != null) {
try {
currentBackgroundImage.close();
Log.v(TAG, "Current background image closed.");
} catch (IOException e) {
Log.e(TAG, "Could not close background image.", e);
}
}

if(bgUri != null && !bgUri.isEmpty()) {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

Activity activity = (Activity) context;
Display display = activity.getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
BitmapFactory.decodeStream( context.getContentResolver().openInputStream(Uri.parse(bgUri)) );
options.inSampleSize = BackgroundImageManager.calculateInSampleSize(options, width, height);
Bitmap bitmap = BitmapFactory.decodeStream( context.getContentResolver().openInputStream(Uri.parse(bgUri)) );
Drawable d = new BitmapDrawable(context.getResources(), bitmap);
return d;
} catch (FileNotFoundException e) {
Log.e(TAG, "Custom background image file could not be found.", e);
} catch (IOException e) {
Log.e(TAG, "Could not close custom background image after creating drawable", e);
}
}
if(bgId != 0) {
try {
return context.getResources().getDrawable(bgId);
} catch (Exception e) {
e.printStackTrace();
}
}
return context.getResources().getDrawable(R.drawable.bg_default);
}

最佳答案

要处理位mpas,您可以使用许多可用的开源库之一。例如Fresco

针对您的问题:

首先,您对同一个位图进行两次解码。

BitmapFactory.decodeStream( context.getContentResolver().openInputStream(Uri.parse(bgUri)) );
options.inSampleSize = BackgroundImageManager.calculateInSampleSize(options, width, height);
Bitmap bitmap = BitmapFactory.decodeStream( context.getContentResolver().openInputStream(Uri.parse(bgUri)) );

这可能是错误的复制/粘贴。在第一行中,位图已解码但未使用。删除第一个 BitmapFactory.decodeStream

问题就出在这里

Bitmap bitmap = BitmapFactory.decodeStream( context.getContentResolver().openInputStream(Uri.parse(bgUri)) );

应该是

Bitmap bitmap = BitmapFactory.decodeStream( context.getContentResolver().openInputStream(Uri.parse(bgUri)), null, options);

选项的对象必须是方法调用的一部分才能使用。

关于java - 有效加载位图 - 仍然出现内存不足错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40406887/

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