我试图缩放位图使其适合,因为某些设备不会显示大于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);
LogCat:
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