0

I'm trying to load an image from the gallery, but toSave.recycle() doesn't work. I mean when toSave.recycle() is called the screen becomes white, the app doesn't crash but the image is not shown. Do you have any suggestions?

Here the code:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == LOAD_IMAGE_RESULT && resultCode == RESULT_OK && data != null) {
        Uri pickedImage = data.getData();
        String[] filePath = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(pickedImage, filePath, null, null, null);
        cursor.moveToFirst();

        String imagePath = cursor.getString(cursor.getColumnIndex(filePath[0]));
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
        postImage.setImageBitmap(bitmap);
        saveImage(bitmap);
        cursor.close();
    }
}

Here the method that converts bitmap image and save it in Firebase.

public void saveImage(Bitmap toSave){
    if (toSave!=null) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        toSave.compress(Bitmap.CompressFormat.JPEG, 50, stream);
        toSave.recycle();
        byte[] byteArray = stream.toByteArray();
        String imageString = Base64.encodeToString(byteArray, Base64.DEFAULT);
        simplepostRef.child("image").setValue(imageString);
    }

Thank you a lot!

Stark_kids
  • 507
  • 1
  • 5
  • 17
  • remove this line *toSave.recycle();* as the same bitmap reference is used to save the image and show in imagefile there is no need to recycle it – Illegal Argument Feb 20 '15 at 10:09

1 Answers1

2

bitmap.recycle() is working in your case. You just dont need to recycle the bitmap because you are showing this bitmap in a imageview.Have a look at the docs.

Illegal Argument
  • 10,090
  • 2
  • 44
  • 61