0

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();
            }
TheFlash
  • 5,997
  • 4
  • 41
  • 46
Yarh
  • 4,459
  • 5
  • 45
  • 95
  • http://stackoverflow.com/a/2508138/1168654 – Dhaval Parmar Jul 02 '13 at 10:51
  • sorry, but what is difference? – Yarh Jul 02 '13 at 11:43
  • 1
    you are trying to convert image in to String... just look in to this ans: http://stackoverflow.com/a/12239626/1168654 – Dhaval Parmar Jul 02 '13 at 11:53
  • I see, i cant recycle if i want to display an image. Befor i though, that calling `recycle` just after setting it to ImageView is ok. And wh y you mentioned string conversion? It is for other purposes and works fine or it interfere with something else? – Yarh Jul 02 '13 at 12:17

1 Answers1

0

Please remove bitmap.recycle(); statement.

nilesh patel
  • 834
  • 1
  • 5
  • 10