1

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

1 回答 1

1

请看看我的相关问题:

高分辨率图像 - OutOfMemoryError

我建议您将图像放入 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

取自:将位图动态放入小部件时失败的活页夹事务

于 2013-08-28T19:59:46.693 回答