从SD卡读取图像时出现内存不足错误

时间:2010-12-01 14:11:15

标签: android

我正在使用ImageView开发一个简单的应用程序,当单击ImageView时,应用程序应该打开设备图库。实现这一点我使用以下代码

    Intent imageIntent=new Intent(Intent.ACTION_PICK);
    imageIntent.setType("image/*");
    startActivityForResult(imageIntent,5);

和ActivityResult

super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 5)
    {
        Cursor cursor;
        Uri uri=data.getData();     
        if(uri!=null)
        {
            cursor=getContentResolver().query(uri, new String[]{ android.provider.MediaStore.Images.ImageColumns.DATA },
                    null, null, null);
            cursor.moveToFirst();
            String imageID=cursor.getString(0);
            personImage.setImageURI(uri);
            Log.i("ImageId", ""+imageID);
            cursor.close();
        }
    }

所选图像显示在ImageView中。现在的问题是,当我第二次这样做时,我得到以下异常。

12-01 19:31:52.344: ERROR/AndroidRuntime(2011): FATAL EXCEPTION: main
12-01 19:31:52.344: ERROR/AndroidRuntime(2011): java.lang.OutOfMemoryError:  bitmap      size exceeds VM budget
12-01 19:31:52.344: ERROR/AndroidRuntime(2011):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:468)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:332)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011):     at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011):     at android.widget.ImageView.resolveUri(ImageView.java:572)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011):     at android.widget.ImageView.setImageURI(ImageView.java:293)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011):     at com.css.testsample.ReadImage.onActivityResult(ReadImage.java:36)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011):     at android.app.Activity.dispatchActivityResult(Activity.java:3931)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3730)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3776)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011):     at android.app.ActivityThread.access$2800(ActivityThread.java:135)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2166)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011):     at android.os.Looper.loop(Looper.java:144)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011):     at android.app.ActivityThread.main(ActivityThread.java:4937)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011):     at java.lang.reflect.Method.invokeNative(Native Method)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011):     at java.lang.reflect.Method.invoke(Method.java:521)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-01 19:31:52.344: ERROR/AndroidRuntime(2011):     at dalvik.system.NativeStart.main(Native Method)

我不知道如何处理这个问题,如果有人知道请帮助我。

谢谢和问候, Rajapandian.K

1 个答案:

答案 0 :(得分:1)

尝试按照here

显示对图像进行下采样