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

时间:2013-08-28 19:53:50

标签: android image bitmap out-of-memory android-drawable

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

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

更新

App仍然崩溃:

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);         

1 个答案:

答案 0 :(得分:1)

请查看我的相关问题:

<强> High resolution Image - OutOfMemoryError

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

确保内存中只有一个Bitmap实例。显示后,调用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