Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android : rotate a high resolution picture generates an out of memory error

I try to rotate a picture in "onPictureTaken"

public void onPictureTaken(byte[] data, Camera camera) {
    String fileName = "last"+Calendar.getInstance().getTimeInMillis();
    String finalFilePath=null;
    File temp= Environment.getExternalStorageDirectory();
    String destination=temp.getAbsolutePath()+"/pictureTest/";
    Bitmap bitmap1 = BitmapFactory.decodeByteArray(data, 0, data.length);
    Matrix mat = new Matrix();
    mat.postRotate(90); // degree j'pense que t'as une methode qui te dit de combien tu veux faire t'as rotation.
    Bitmap bBitmapRotate = Bitmap.createBitmap(bitmap1, 0, 0, bitmap1.getWidth(), bitmap1.getHeight(), mat, true);
    try {
        FileOutputStream fos = null;
        finalFilePath=destination +"/" + fileName + ".jpg";
        fos = new FileOutputStream(finalFilePath);
        BufferedOutputStream bos = new BufferedOutputStream(fos);                   
        bBitmapRotate.compress(CompressFormat.JPEG, 80, bos);
        bos.flush();
        bos.close();
    } 
    catch (FileNotFoundException e) {
        e.printStackTrace();
    } 
    catch (IOException e) {
        e.printStackTrace();
    }

It works fine if i use a 2Mp picture but if I use a 12Mp picture I get an out of memory error.

    05-22 11:08:41.180: D/dalvikvm(29694): GC_FOR_MALLOC freed 674K, 51% free 3015K/6087K, external 18826K/19988K, paused 33ms
05-22 11:08:41.190: I/dalvikvm-heap(29694): Grow heap (frag case) to 25.641MB for 2070498-byte allocation
05-22 11:08:41.230: D/dalvikvm(29694): GC_CONCURRENT freed 133K, 40% free 4903K/8135K, external 17940K/19988K, paused 1ms+2ms
05-22 11:08:41.700: D/dalvikvm(29694): GC_EXTERNAL_ALLOC freed 19K, 40% free 4884K/8135K, external 33575K/35623K, paused 24ms
05-22 11:08:41.710: E/dalvikvm-heap(29694): 24000000-byte external allocation too large for this process.
05-22 11:08:41.710: E/GraphicsJNI(29694): VM won't let us allocate 24000000 bytes
05-22 11:08:41.720: D/dalvikvm(29694): GC_FOR_MALLOC freed <1K, 40% free 4884K/8135K, external 33575K/35623K, paused 14ms
05-22 11:08:41.720: D/AndroidRuntime(29694): Shutting down VM
05-22 11:08:41.720: W/dalvikvm(29694): threadid=1: thread exiting with uncaught exception (group=0x2aac8578)
05-22 11:08:41.720: E/AndroidRuntime(29694): FATAL EXCEPTION: main
05-22 11:08:41.720: E/AndroidRuntime(29694): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
05-22 11:08:41.720: E/AndroidRuntime(29694):    at android.graphics.Bitmap.nativeCreate(Native Method)
05-22 11:08:41.720: E/AndroidRuntime(29694):    at android.graphics.Bitmap.createBitmap(Bitmap.java:477)
05-22 11:08:41.720: E/AndroidRuntime(29694):    at android.graphics.Bitmap.createBitmap(Bitmap.java:444)
05-22 11:08:41.720: E/AndroidRuntime(29694):    at com.android.cocoatweet.TestTweetPictureActivity.onPictureTaken(TestTweetPictureActivity.java:143)
05-22 11:08:41.720: E/AndroidRuntime(29694):    at android.hardware.Camera$EventHandler.handleMessage(Camera.java:529)
05-22 11:08:41.720: E/AndroidRuntime(29694):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 11:08:41.720: E/AndroidRuntime(29694):    at android.os.Looper.loop(Looper.java:138)
05-22 11:08:41.720: E/AndroidRuntime(29694):    at android.app.ActivityThread.main(ActivityThread.java:3701)
05-22 11:08:41.720: E/AndroidRuntime(29694):    at java.lang.reflect.Method.invokeNative(Native Method)
05-22 11:08:41.720: E/AndroidRuntime(29694):    at java.lang.reflect.Method.invoke(Method.java:507)
05-22 11:08:41.720: E/AndroidRuntime(29694):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
05-22 11:08:41.720: E/AndroidRuntime(29694):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
05-22 11:08:41.720: E/AndroidRuntime(29694):    at dalvik.system.NativeStart.main(Native Method)

I've tried to set the "setRotation()" from the camera parameters but it didn't do anything.

public void onClick(View v) {
                sendPicturedTweet();
            }
        });
    }

    protected void sendPicturedTweet() {
        if(camera!=null){
            camera.stopPreview();
            Camera.Parameters camParams=this.camera.getParameters();            
            List<Size> sizes=camParams.getSupportedPictureSizes();
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
            int prefSize=Integer.parseInt(preferences.getString(getString(R.string.picture_quality_key), "0"));
            camParams.setPictureSize(sizes.get(prefSize).width, sizes.get(prefSize).height);
            camParams.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
            camParams.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
            camParams.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
            camParams.setPictureFormat(ImageFormat.JPEG);
            camParams.setJpegQuality(100);
            camParams.setRotation(90);
            camera.setParameters(camParams);
            try{
                this.camera.setPreviewDisplay(surfaceHolder);
                this.camera.startPreview();
                this.camera.takePicture(null, null, null, this);
            }catch (Exception e) {
                Log.e(LOG_TAG_VIDEO_PREVIEW, "ERROR IO on set preview display for camera.");
            }
        }

    }

How can I rotate a large picture without having this error and having the best picture quality possible? Is it possible?

I am working on a Sony Xperia S with Android 2.3.7.

Thanks.

like image 286
cooler007 Avatar asked May 22 '12 12:05

cooler007


2 Answers

When you reach this line

Bitmap bBitmapRotate = Bitmap.createBitmap(bitmap1, 0, 0, bitmap1.getWidth(), bitmap1.getHeight(), mat, true);

you already have the image in the memory twice, as byte[] data and as Bitmap bitmap1. I think that you should find a way to clear byte[] data before you try to transform the image (e.g. by sending directly the bitmap as parameter)

like image 109
tibtof Avatar answered Oct 29 '22 04:10

tibtof


Reduce the Size of image using BitmapFactory.Option

BitmapFactory.Options option = new BitmapFactory.Options();
            option.inSampleSize = 2;
like image 41
Sachin Gurnani Avatar answered Oct 29 '22 03:10

Sachin Gurnani