开发者

Getting Out of memory error when reading images from SDcard

I am developing a simple application with an ImageView, when the ImageView is clicked the application should open the device Image gallery. to achieve this i am using the following code

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

And the 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();
        }
    }

The selected image is displayed in the ImageView. Now the problem is when i am doing it second time i am getting the following exception.

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:5开发者_开发问答2.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)

I dont know how to handle this problem, if anybody knows it please help me.

Thanks and Regards, Rajapandian.K


Try downsampling your images as shown here

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜