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