src/com/alcoapps/imageutility/ImageutilityModule.java
@Kroll.method
    public Boolean rotateResizeImage(String filename, int size, int quality)
    {
        Log.i(TAG, "In rotateResizeImage");
        File imageFile = null;
        FileInputStream fileInputStream = null;
        FileInputStream fileInputStream2 = null;
        FileOutputStream fileOutputStream = null;
        Bitmap scaledBitmap = null;
        Bitmap resizedBitmap = null;
        try
        {
            // Determine the orientation
            int rotation = getRotation(filename);
 
            // Read the file path into a File
            imageFile = new File(convertPath(filename));
 
            // Decode once to determine size
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inJustDecodeBounds = true;
            fileInputStream = new FileInputStream(imageFile);
            BitmapFactory.decodeStream(fileInputStream, null, opts);
            Log.i(TAG, "Original image size: " + opts.outWidth + "x" + opts.outHeight);
 
            // Determine scaling based on size
            int sample = 1;
            while (opts.outWidth / sample / 2 >= size || opts.outHeight / sample / 2 >= size) {
                sample *= 2;
            }
            opts = new BitmapFactory.Options();
            opts.inSampleSize = sample;
            Log.i(TAG, "Sample size: " + sample);
 
            // Decode with scaling applied to save a huge amount of memory
            fileInputStream2 = new FileInputStream(imageFile);
            scaledBitmap = BitmapFactory.decodeStream(fileInputStream2, null, opts);
            Log.i(TAG, "Sampled image size: " + opts.outWidth + "x" + opts.outHeight);
 
            // Create the appropriate Matrix to resize and rotate then create Bitmap
            float scale = (float)size / (float)(opts.outWidth > opts.outHeight ? opts.outWidth : opts.outHeight);
            Matrix matrix = new Matrix();
            matrix.postRotate(rotation);
            matrix.postScale(scale, scale);
            resizedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, (int)scaledBitmap.getWidth(), (int)scaledBitmap.getHeight(), matrix, true);
            Log.i(TAG, "Scaled image size: " + resizedBitmap.getWidth() + "x" + resizedBitmap.getHeight());
 
            // Delete old file
            imageFile.delete();
            Log.i(TAG,  "Deleted old file");
 
            // Create new file
            imageFile.createNewFile();
            Log.i(TAG,  "Created new file");
 
            // Copy rotated, resized image to new file
            fileOutputStream = new FileOutputStream(imageFile);
            resizedBitmap.compress(Bitmap.CompressFormat.JPEG, quality, fileOutputStream);
            fileOutputStream.flush();
            Log.i(TAG,  "Copied rotated, resized image to new file");
 
            return true;
        }
        catch (Exception e)
        {
            Log.e(TAG, "ERROR: " + e.toString());
            return false;
        }
        finally {
            if(imageFile != null) {
                imageFile = null;
            }
            if(fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                }
                fileInputStream = null;
            }
            if(fileInputStream2 != null) {
                try {
                    fileInputStream2.close();
                } catch (IOException e) {
                }
                fileInputStream2 = null;
            }
            if(fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                }
                fileOutputStream = null;
            }
            if(scaledBitmap != null) {
                scaledBitmap.recycle();
                scaledBitmap = null;
            }
            if(resizedBitmap != null) {
                resizedBitmap.recycle();
                resizedBitmap = null;
            }
        }
    }
/**
     * Auto rotate and resize to the desired maximum size keeping aspect
     *
     * @param filename
     * @param size
     * @param quality
     * @return
     */
Auto rotate and resize to the desired maximum size keeping aspect
@param filename
@param size
@param quality
@return
Auto rotate and resize to the desired maximum size keeping aspect @param filename @param size @param quality @return
@Kroll.method
    public Boolean rotateResizeImage(String filename, int size, int quality)
    {
        Log.i(TAG, "In rotateResizeImage");
        File imageFile = null;
        FileInputStream fileInputStream = null;
        FileInputStream fileInputStream2 = null;
        FileOutputStream fileOutputStream = null;
        Bitmap scaledBitmap = null;
        Bitmap resizedBitmap = null;
        try
        {
            int rotation = getRotation(filename);
            imageFile = new File(convertPath(filename));
            BitmapFactory.Options opts = new BitmapFactory.Options();
            opts.inJustDecodeBounds = true;
            fileInputStream = new FileInputStream(imageFile);
            BitmapFactory.decodeStream(fileInputStream, null, opts);
            Log.i(TAG, "Original image size: " + opts.outWidth + "x" + opts.outHeight);
            int sample = 1;
            while (opts.outWidth / sample / 2 >= size || opts.outHeight / sample / 2 >= size) {
                sample *= 2;
            }
            opts = new BitmapFactory.Options();
            opts.inSampleSize = sample;
            Log.i(TAG, "Sample size: " + sample);
            fileInputStream2 = new FileInputStream(imageFile);
            scaledBitmap = BitmapFactory.decodeStream(fileInputStream2, null, opts);
            Log.i(TAG, "Sampled image size: " + opts.outWidth + "x" + opts.outHeight);
            float scale = (float)size / (float)(opts.outWidth > opts.outHeight ? opts.outWidth : opts.outHeight);
            Matrix matrix = new Matrix();
            matrix.postRotate(rotation);
            matrix.postScale(scale, scale);
            resizedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, (int)scaledBitmap.getWidth(), (int)scaledBitmap.getHeight(), matrix, true);
            Log.i(TAG, "Scaled image size: " + resizedBitmap.getWidth() + "x" + resizedBitmap.getHeight());
            imageFile.delete();
            Log.i(TAG,  "Deleted old file");
            imageFile.createNewFile();
            Log.i(TAG,  "Created new file");
            fileOutputStream = new FileOutputStream(imageFile);
            resizedBitmap.compress(Bitmap.CompressFormat.JPEG, quality, fileOutputStream);
            fileOutputStream.flush();
            Log.i(TAG,  "Copied rotated, resized image to new file");
            return true;
        }
        catch (Exception e)
        {
            Log.e(TAG, "ERROR: " + e.toString());
            return false;
        }
        finally {
            if(imageFile != null) {
                imageFile = null;
            }
            if(fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                }
                fileInputStream = null;
            }
            if(fileInputStream2 != null) {
                try {
                    fileInputStream2.close();
                } catch (IOException e) {
                }
                fileInputStream2 = null;
            }
            if(fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                }
                fileOutputStream = null;
            }
            if(scaledBitmap != null) {
                scaledBitmap.recycle();
                scaledBitmap = null;
            }
            if(resizedBitmap != null) {
                resizedBitmap.recycle();
                resizedBitmap = null;
            }
        }
    }
@ Kroll . method public Boolean rotateResizeImage ( String filename , int size , int quality ) { Log . i ( TAG , "In rotateResizeImage" ) ; File imageFile = null ; FileInputStream fileInputStream = null ; FileInputStream fileInputStream2 = null ; FileOutputStream fileOutputStream = null ; Bitmap scaledBitmap = null ; Bitmap resizedBitmap = null ; try { int rotation = getRotation ( filename ) ; imageFile = new File ( convertPath ( filename ) ) ; BitmapFactory . Options opts = new BitmapFactory . Options ( ) ; opts . inJustDecodeBounds = true ; fileInputStream = new FileInputStream ( imageFile ) ; BitmapFactory . decodeStream ( fileInputStream , null , opts ) ; Log . i ( TAG , "Original image size: " + opts . outWidth + "x" + opts . outHeight ) ; int sample = 1 ; while ( opts . outWidth / sample / 2 >= size || opts . outHeight / sample / 2 >= size ) { sample *= 2 ; } opts = new BitmapFactory . Options ( ) ; opts . inSampleSize = sample ; Log . i ( TAG , "Sample size: " + sample ) ; fileInputStream2 = new FileInputStream ( imageFile ) ; scaledBitmap = BitmapFactory . decodeStream ( fileInputStream2 , null , opts ) ; Log . i ( TAG , "Sampled image size: " + opts . outWidth + "x" + opts . outHeight ) ; float scale = ( float ) size / ( float ) ( opts . outWidth > opts . outHeight ? opts . outWidth : opts . outHeight ) ; Matrix matrix = new Matrix ( ) ; matrix . postRotate ( rotation ) ; matrix . postScale ( scale , scale ) ; resizedBitmap = Bitmap . createBitmap ( scaledBitmap , 0 , 0 , ( int ) scaledBitmap . getWidth ( ) , ( int ) scaledBitmap . getHeight ( ) , matrix , true ) ; Log . i ( TAG , "Scaled image size: " + resizedBitmap . getWidth ( ) + "x" + resizedBitmap . getHeight ( ) ) ; imageFile . delete ( ) ; Log . i ( TAG , "Deleted old file" ) ; imageFile . createNewFile ( ) ; Log . i ( TAG , "Created new file" ) ; fileOutputStream = new FileOutputStream ( imageFile ) ; resizedBitmap . compress ( Bitmap . CompressFormat . JPEG , quality , fileOutputStream ) ; fileOutputStream . flush ( ) ; Log . i ( TAG , "Copied rotated, resized image to new file" ) ; return true ; } catch ( Exception e ) { Log . e ( TAG , "ERROR: " + e . toString ( ) ) ; return false ; } finally { if ( imageFile != null ) { imageFile = null ; } if ( fileInputStream != null ) { try { fileInputStream . close ( ) ; } catch ( IOException e ) { } fileInputStream = null ; } if ( fileInputStream2 != null ) { try { fileInputStream2 . close ( ) ; } catch ( IOException e ) { } fileInputStream2 = null ; } if ( fileOutputStream != null ) { try { fileOutputStream . close ( ) ; } catch ( IOException e ) { } fileOutputStream = null ; } if ( scaledBitmap != null ) { scaledBitmap . recycle ( ) ; scaledBitmap = null ; } if ( resizedBitmap != null ) { resizedBitmap . recycle ( ) ; resizedBitmap = null ; } } }
Auto rotate and resize to the desired maximum size keeping aspect
 @param filename
 @param size
 @param quality
 @return
Auto rotate and resize to the desired maximum size keeping aspect @param filename @param size @param quality @return
// Determine the orientation // Read the file path into a File // Decode once to determine size // Determine scaling based on size // Decode with scaling applied to save a huge amount of memory // Create the appropriate Matrix to resize and rotate then create Bitmap // Delete old file // Create new file // Copy rotated, resized image to new file