gpt4 book ai didi

android - 加载位图时出现内存不足错误

转载 作者:行者123 更新时间:2023-11-29 20:08:13 25 4
gpt4 key购买 nike

执行此代码时出现内存不足异常,

Back.setImageBitmap(decodeSampledBitmapFromUri(myUri, 50, 50));

我使用下面的代码来控制这个错误,它工作正常但是对于更大的图像这个错误仍然存​​在。

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) {
if (width > height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}

public Bitmap decodeSampledBitmapFromUri(Uri uri,int reqWidth, int reqHeight) throws FileNotFoundException {


// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
}

此处完整错误:

02-12 14:14:51.888: E/AndroidRuntime(6560): FATAL EXCEPTION: main
02-12 14:14:51.888: E/AndroidRuntime(6560): java.lang.OutOfMemoryError
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:530)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:603)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.app.cut.BlurIt.decodeSampledBitmapFromUri(BlurIt.java:938)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.app.cut.BlurIt.onCreate(BlurIt.java:223)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.app.Activity.performCreate(Activity.java:5133)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.app.ActivityThread.access$600(ActivityThread.java:150)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.os.Looper.loop(Looper.java:213)
02-12 14:14:51.888: E/AndroidRuntime(6560): at android.app.ActivityThread.main(ActivityThread.java:5225)
02-12 14:14:51.888: E/AndroidRuntime(6560): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 14:14:51.888: E/AndroidRuntime(6560): at java.lang.reflect.Method.invoke(Method.java:525)
02-12 14:14:51.888: E/AndroidRuntime(6560): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
02-12 14:14:51.888: E/AndroidRuntime(6560): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
02-12 14:14:51.888: E/AndroidRuntime(6560): at dalvik.system.NativeStart.main(Native Method)

第 938 行:

BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));

第 223 行:

Back.setImageBitmap(decodeSampledBitmapFromUri(myUri, 10, 10));

更新:

我用这个

InputStream in = null;

in = getContentResolver().openInputStream(uri);
BitmapFactory.decodeStream(in, null, options);

代替这一行

    BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));

它可以正常工作并加载图像而不会出现内存不足的问题,但我不知道为什么它返回的位图与我所说的不完全一样。我调用 (100,100) 返回 (172,171),实际大小是 (5508, 5472)。

最佳答案

您可以使用 Glide 或 Picasso 库将位图加载到 ImageView。

Glide.with(context).load(urlOrUri).into(imageViewToLoad);

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

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