gpt4 book ai didi

android - 尝试重新缩放位图时出现 OutOfMemory 错误

转载 作者:行者123 更新时间:2023-11-30 03:24:26 25 4
gpt4 key购买 nike

我正在尝试缩放位图以使其适合,因为某些设备不会显示 > 2048 的图像。

我找到了一个解决方案,但是当我选择/拍照时应用程序崩溃了。

那段代码:(在onActivityResult里面)

currImageURI = data.getData();
path=(getRealPathFromURI(currImageURI));

Drawable d = Drawable.createFromPath(path);
image.setVisibility(View.VISIBLE);
mPhoto=drawableToBitmap(d);
Bitmap sized = mPhoto;
if(mPhoto.getHeight()>=2048||mPhoto.getWidth()>=2048){
sized = Bitmap.createScaledBitmap(mPhoto, (int)(mPhoto.getWidth()*0.8), (int)(mPhoto.getHeight()*0.8), false);
}

image.setImageBitmap(sized);

日志:

08-28 22:48:14.873: E/AndroidRuntime(12047): FATAL EXCEPTION: main
08-28 22:48:14.873: E/AndroidRuntime(12047): java.lang.OutOfMemoryError
08-28 22:48:14.873: E/AndroidRuntime(12047): at android.graphics.Bitmap.nativeCreate(Native Method)
08-28 22:48:14.873: E/AndroidRuntime(12047): at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
08-28 22:48:14.873: E/AndroidRuntime(12047): at android.graphics.Bitmap.createBitmap(Bitmap.java:586)
08-28 22:48:14.873: E/AndroidRuntime(12047): at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:466)
08-28 22:48:14.873: E/AndroidRuntime(12047): at com.example.free.Add.onActivityResult(Add.java:149)
08-28 22:48:14.873: E/AndroidRuntime(12047): at android.app.Activity.dispatchActivityResult(Activity.java:5231)
08-28 22:48:14.873: E/AndroidRuntime(12047): at android.app.ActivityThread.deliverResults(ActivityThread.java:3172)
08-28 22:48:14.873: E/AndroidRuntime(12047): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3219)
08-28 22:48:14.873: E/AndroidRuntime(12047): at android.app.ActivityThread.access$1200(ActivityThread.java:134)
08-28 22:48:14.873: E/AndroidRuntime(12047): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
08-28 22:48:14.873: E/AndroidRuntime(12047): at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 22:48:14.873: E/AndroidRuntime(12047): at android.os.Looper.loop(Looper.java:137)
08-28 22:48:14.873: E/AndroidRuntime(12047): at android.app.ActivityThread.main(ActivityThread.java:4856)
08-28 22:48:14.873: E/AndroidRuntime(12047): at java.lang.reflect.Method.invokeNative(Native Method)
08-28 22:48:14.873: E/AndroidRuntime(12047): at java.lang.reflect.Method.invoke(Method.java:511)
08-28 22:48:14.873: E/AndroidRuntime(12047): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
08-28 22:48:14.873: E/AndroidRuntime(12047): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
08-28 22:48:14.873: E/AndroidRuntime(12047): at dalvik.system.NativeStart.main(Native Method)

第 149 行是:

sized = Bitmap.createScaledBitmap(mPhoto, (int)(mPhoto.getWidth()*0.8), (int)(mPhoto.getHeight()*0.8), false);

你能告诉我怎样做才能防止它崩溃吗?谢谢!

更新:

应用仍然崩溃:

08-28 23:24:20.219: W/dalvikvm(16289): threadid=1: thread exiting with uncaught exception (group=0x412552a0)   
08-28 23:18:59.917: E/AndroidRuntime(15286): FATAL EXCEPTION: main
08-28 23:18:59.917: E/AndroidRuntime(15286): java.lang.OutOfMemoryError
08-28 23:18:59.917: E/AndroidRuntime(15286): at android.graphics.Bitmap.nativeCreate(Native Method)
08-28 23:18:59.917: E/AndroidRuntime(15286): at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
08-28 23:18:59.917: E/AndroidRuntime(15286): at android.graphics.Bitmap.createBitmap(Bitmap.java:586)
08-28 23:18:59.917: E/AndroidRuntime(15286): at com.example.free.Add.getResizedBitmap(Add.java:335)
08-28 23:18:59.917: E/AndroidRuntime(15286): at com.example.free.Add.onActivityResult(Add.java:150)
08-28 23:18:59.917: E/AndroidRuntime(15286): at android.app.Activity.dispatchActivityResult(Activity.java:5231)
08-28 23:18:59.917: E/AndroidRuntime(15286): at android.app.ActivityThread.deliverResults(ActivityThread.java:3172)
08-28 23:18:59.917: E/AndroidRuntime(15286): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3219)
08-28 23:18:59.917: E/AndroidRuntime(15286): at android.app.ActivityThread.access$1200(ActivityThread.java:134)
08-28 23:18:59.917: E/AndroidRuntime(15286): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
08-28 23:18:59.917: E/AndroidRuntime(15286): at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 23:18:59.917: E/AndroidRuntime(15286): at android.os.Looper.loop(Looper.java:137)
08-28 23:18:59.917: E/AndroidRuntime(15286): at android.app.ActivityThread.main(ActivityThread.java:4856)
08-28 23:18:59.917: E/AndroidRuntime(15286): at java.lang.reflect.Method.invokeNative(Native Method)
08-28 23:18:59.917: E/AndroidRuntime(15286): at java.lang.reflect.Method.invoke(Method.java:511)
08-28 23:18:59.917: E/AndroidRuntime(15286): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
08-28 23:18:59.917: E/AndroidRuntime(15286): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
08-28 23:18:59.917: E/AndroidRuntime(15286): at dalvik.system.NativeStart.main(Native Method)

代码:

currImageURI = data.getData();
path=(getRealPathFromURI(currImageURI));

Drawable d = Drawable.createFromPath(path);
image.setVisibility(View.VISIBLE);
mPhoto=drawableToBitmap(d);
Bitmap sized = mPhoto;
if(mPhoto.getHeight()>=2048||mPhoto.getWidth()>=2048){
sized=getResizedBitmap(mPhoto,(int)(mPhoto.getHeight()*0.8),(int)(mPhoto.getWidth()*0.8));
}

image.setImageBitmap(sized);

最佳答案

请看我的相关问题:

High resolution Image - OutOfMemoryError

我建议您将图像放入 Drawables 文件夹中。从那里,使用下面的代码以适当的大小加载它。这将减少您的应用程序的内存使用量,并且始终只使用所需的内存量。

确保内存中只有一个位图实例。显示后,调用 recycle() 并将您的引用设置为 null。

这是加载图片的方式:

public  Bitmap scaleDownBitmap(Bitmap photo, int newHeight) {

final float densityMultiplier = getResources().getDisplayMetrics().density;

int h= (int) (newHeight*densityMultiplier);
int w= (int) (h * photo.getWidth()/((double) photo.getHeight()));

photo = Bitmap.createScaledBitmap(photo, w, h, true);

return photo;
}

像这样使用它:

ImageView.setImageBitmap(scaleDownBitmap(youroldbitmap, 300));

请注意不要为您的 newHeight 使用太高的值,并记住 densityMultiplier

取自:Failed binder transaction when putting an bitmap dynamically in a widget

关于android - 尝试重新缩放位图时出现 OutOfMemory 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18497065/

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