0

I'm new to using Bitmaps and don't completely understand why I'm running out of memory. I want to save an image of which the path is passed back to an activity from the camera app like so:

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            adapter.saveResizedImage(mCurrentPhotoPath);
        }
 }

Calling the adapter method:

 public void saveResizedImage(String path){
        BitmapFactory.Options bmOptions = new BitmapFactory.Options();
        Bitmap photo = (Bitmap) BitmapFactory.decodeFile(path, bmOptions);
        photo = Bitmap.createScaledBitmap(photo, INSTAGRAM_FORMAT_H, INSTAGRAM_FORMAT_W, false);
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

        File f = new File(path);

        try {
            f.createNewFile();
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(bytes.toByteArray());
            fo.close();
            TernaryActivity.mCurrentPhotoPath = f.getAbsolutePath();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Exception occurs at Bitmap photo = (Bitmap) BitmapFactory.decodeFile(path, bmOptions);

I should mention that the activity which calls onActivityResult() can contain up to 6 images and that the error occurs when I took the 5th picture and it was passed back to the activity.

sqlbuddy
  • 79
  • 1
  • 10
  • how about search before ask? – Stan Apr 23 '15 at 19:08
  • Try adding a `photo.recycle()` call at the end to manually release the memory. http://developer.android.com/reference/android/graphics/Bitmap.html#recycle() – Buddy Apr 23 '15 at 19:08
  • look at this answer: [link](http://stackoverflow.com/questions/16284725/how-to-avoid-out-of-memory-exception-when-doing-bitmap-processing). @Stan is right, try to search before ask. – MiguelHincapieC Apr 23 '15 at 19:11

0 Answers0