2

I have an app which takes pictures and then displays them in the app. The first image taken works and is shown in the app, however when the second images is taken the app crashes and I get the error in the title in logcat.

p.s It is code written by a friend so I'm not 100% sure about it.

code

private PictureCallback mPicture = new PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        mImageView = (ImageView) findViewById(R.id.mImageView);
        Bitmap imageBitmap = BitmapFactory.decodeByteArray(data, 0,
                data.length);

        mImageView.setImageBitmap(imageBitmap);

        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        if (pictureFile == null) {
            /*
             * Log.d(TAG,
             * "Error creating media file, check storage permissions: " +
             * e.getMessage());
             */
            return;
        }
        try {
            SharedPreferences save = getPreferences(0);
            SharedPreferences.Editor editor = save.edit(); 
            editor.putString("oldFile", pictureFile.getAbsolutePath());

            // Commit the edits!
            editor.commit();
            Log.v("output", "oldFile: " + oldFilePath);
            File oldFile = new File(oldFilePath);
            if(oldFile.delete()) // DELETING PICTURES TOO FAST.
                Log.v(TAG, "Image deleted.");
            oldFilePath = pictureFile.getAbsolutePath();
            Log.v("output", "newFile: " + oldFilePath);

            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
        } catch (FileNotFoundException e) {
            Log.d(TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d(TAG, "Error accessing file: " + e.getMessage());
        }
    }
};

logcat

02-19 14:22:08.158: E/dalvikvm-heap(10394): Out of memory on a 31961104-byte allocation. 02-19 14:22:08.163: E/AndroidRuntime(10394): FATAL EXCEPTION: main 02-19 14:22:08.163: E/AndroidRuntime(10394): java.lang.OutOfMemoryError 02-19 14:22:08.163: E/AndroidRuntime(10394): at android.graphics.BitmapFactory.nativeDecodeByteArray(Native Method) 02-19 14:22:08.163: E/AndroidRuntime(10394): at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:551) 02-19 14:22:08.163: E/AndroidRuntime(10394): at android.graphics.BitmapFactory.decodeByteArray(BitmapFactory.java:569) 02-19 14:22:08.163: E/AndroidRuntime(10394): at com.example.oxplastics.MainActivity$1.onPictureTaken(MainActivity.java:331) 02-19 14:22:08.163: E/AndroidRuntime(10394): at android.hardware.Camera$EventHandler.handleMessage(Camera.java:823) 02-19 14:22:08.163: E/AndroidRuntime(10394): at android.os.Handler.dispatchMessage(Handler.java:99) 02-19 14:22:08.163: E/AndroidRuntime(10394): at android.os.Looper.loop(Looper.java:137) 02-19 14:22:08.163: E/AndroidRuntime(10394): at android.app.ActivityThread.main(ActivityThread.java:4921) 02-19 14:22:08.163: E/AndroidRuntime(10394): at java.lang.reflect.Method.invokeNative(Native Method) 02-19 14:22:08.163: E/AndroidRuntime(10394): at java.lang.reflect.Method.invoke(Method.java:511) 02-19 14:22:08.163: E/AndroidRuntime(10394): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027) 02-19 14:22:08.163: E/AndroidRuntime(10394): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 02-19 14:22:08.163: E/AndroidRuntime(10394): at dalvik.system.NativeStart.main(Native Method)

2
  • 1
    post your code, also! Commented Feb 19, 2014 at 14:27
  • 1
    My God, how many duplicates of this question will it take before people start looking at other questions and stop posting OOM related to Bitmaps ? Commented Feb 19, 2014 at 14:30

1 Answer 1

3

you should sample down the image before displaying them on screen , look at following link

http://developer.android.com/training/displaying-bitmaps/index.html.

now its possible that first time when your bitmap object is created , your device was having sufficient memory , but when next object is created its going out of memory !!

Sign up to request clarification or add additional context in comments.

8 Comments

Also there are 3rd party libraries that make it easier for you to implement the best practices that are presented on that page. Universal-Image-Loader is one such library.
Just scaling the image down to a smaller file wont be sufficient. How do I clear the cache/memory of the previous image
I am trying to use recycle() to clear the memory but not sure what to call it on or where to put it.
please go through that link , btw u can call bitmap.recycle() more reference developer.android.com/reference/android/graphics/Bitmap.html
u can put it, after closing the file stream
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.