Question

I select 700kb image from gallery and recieve IllegalArgumentException. I link this with ussage of recycle() to save some memory. It bacomes actual problem as i started to get OutOfMemoryError, so i think i have to use recycle. Did I called it in wrong way or there is some rules how to use it propery?

error:

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)

call:

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();
            }
Was it helpful?

Solution

Please remove bitmap.recycle(); statement.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top