从图库中选择图像时强制关闭

时间:2012-02-16 07:16:03

标签: android

在我的程序中,我试图从图像gallary中选择图像。问题是在尝试连续两到三次选择新的imga时我得到“位图大小超过VM预算”异常

这是我的代码onActivity result

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
        case SELECT_IMAGE:
            String imagePath = getPath(data.getData());
            Savingimagepath(imagePath);
            img.setImageDrawable(Drawable.createFromPath(imagePath));
            break;
        }
    }
}

任何人都可以帮助解决这个问题...

这是我的原木猫......

02-16 12:16:57.870: W/dalvikvm(2198): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-16 12:16:57.870: E/AndroidRuntime(2198): FATAL EXCEPTION: main
02-16 12:16:57.870: E/AndroidRuntime(2198): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
02-16 12:16:57.870: E/AndroidRuntime(2198):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
02-16 12:16:57.870: E/AndroidRuntime(2198):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:470)
02-16 12:16:57.870: E/AndroidRuntime(2198):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:284)
02-16 12:16:57.870: E/AndroidRuntime(2198):     at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:309)
02-16 12:16:57.870: E/AndroidRuntime(2198):     at android.graphics.drawable.Drawable.createFromPath(Drawable.java:800)
02-16 12:16:57.870: E/AndroidRuntime(2198):     at palmagent.FidelityAgent.CA.contact.onActivityResult(contact.java:249)
02-16 12:16:57.870: E/AndroidRuntime(2198):     at android.app.Activity.dispatchActivityResult(Activity.java:3908)
02-16 12:16:57.870: E/AndroidRuntime(2198):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
02-16 12:16:57.870: E/AndroidRuntime(2198):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
02-16 12:16:57.870: E/AndroidRuntime(2198):     at android.app.ActivityThread.access$2000(ActivityThread.java:117)
02-16 12:16:57.870: E/AndroidRuntime(2198):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
02-16 12:16:57.870: E/AndroidRuntime(2198):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 12:16:57.870: E/AndroidRuntime(2198):     at android.os.Looper.loop(Looper.java:130)
02-16 12:16:57.870: E/AndroidRuntime(2198):     at android.app.ActivityThread.main(ActivityThread.java:3683)
02-16 12:16:57.870: E/AndroidRuntime(2198):     at java.lang.reflect.Method.invokeNative(Native Method)
02-16 12:16:57.870: E/AndroidRuntime(2198):     at java.lang.reflect.Method.invoke(Method.java:507)
02-16 12:16:57.870: E/AndroidRuntime(2198):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-16 12:16:57.870: E/AndroidRuntime(2198):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-16 12:16:57.870: E/AndroidRuntime(2198):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

您应该创建一个drawable以完全适合ImageView(img)大小。如果您打开一个大图像(如照片)并将其设置为ImageView,它需要大量内存。

只需创建一个真正具有此目标尺寸的位图

BitmapFactory.Options opts = new BitmapFactory.Options();
//The max size of any dimension (height or width) is 400 px
opts.inSampleSize = originalSize / 400;
Bitmap bmp = BitmapFactory.decodeFile(imagePath, opts);
img.setImageBitmap(bmp);
img.recycle();

答案 1 :(得分:0)

有很多相同的链接。尝试搜索here