从库中选择图像时出错

时间:2013-07-02 10:36:25

标签: android illegalargumentexception recycle

我从图库中选择700kb图像并收到IllegalArgumentException。我将其与recycle()的ussage链接以节省一些内存。当我开始得到OutOfMemoryError时,它会出现实际问题,因此我认为我必须使用回收。我是以错误的方式调用它还是有一些规则如何使用它?

错误:

07-02 10:25:27.466: E/AndroidRuntime(2422): FATAL EXCEPTION: main
07-02 10:25:27.466: E/AndroidRuntime(2422): java.lang.IllegalArgumentException: Cannot draw recycled bitmaps
07-02 10:25:27.466: E/AndroidRuntime(2422):     at android.view.GLES20Canvas.drawBitmap(GLES20Canvas.java:778)
07-02 10:25:27.466: E/AndroidRuntime(2422):     at android.view.GLES20RecordingCanvas.drawBitmap(GLES20RecordingCanvas.java:117)

呼叫:

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(
intent, "Select Picture"), 31);

onResult:

if (requestCode == 31 && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
            pho1.setImageBitmap(bitmap);
            // ImageView imageView = (ImageView) findViewById(R.id.imgView);
            // imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
            //

            // Bitmap bitmap = (Bitmap) data.getExtras().get("data");
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
            byte[] byte_arr = stream.toByteArray();

            photo1 = Base64.encodeToString(byte_arr, Base64.DEFAULT);
            bitmap.recycle();
            bitmap = null;
            byte_arr=null;

            try {
                stream.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

1 个答案:

答案 0 :(得分:0)

请删除bitmap.recycle();言。