Wednesday, 23 March 2016

Marshmellow Run-time Permission Model


Hello Friends,

            Android Marshmello 6.0 has introduced a new permission model,which checks and allows permissions at run time rather then app installation time. Users now will be able to grant the permissions while running an application rather than app installation time. There are almost 130 unique permissions available to Android developers.

        
            An applications permissions can be controlled from the App Info > App permissions section. Earlier we simply declared permissions in AndroidManifest.xml and we were on our way. But now apart from that, we need to check every time for a permission related task. Such as requesting the camera, calendar, contacts and so on. In addition, if the user denies the request permission, we need to handle that too.

Good for users,not for developers
                       
                  In pre marshmellow, all permissions  had to be granted during install time. The user has no choice but to accept them. But that has changed now. Runtime permissions give users control over the sensitive information they provide. They choose which apps can access what and when they cannot.

Earlier we simply declared permissions in AndroidManifest.xml file and we were on our way. But now apart from that, we need to check everytime for a permission related task. Such as requesting camera,calendars,contacts and so on. In addition, if user denies the request permission, we need to handle that too.

You might be wonder what about the pre Marshmellow versions devices? 

Don't worry.

                Android team has already thought about it.  If application's targetSdkVersion  is set to less than 23. It will be assumed that application is not tested with new permission system yet and will switch to the same old behavior: user has to accept every single permission at install time and they will be all granted one installed!

In Android 6.0 Marshmellow ,application will not be granted any permission at installation time. Instead, application has to ask user for a permission one-by-one at runtime.

Please note that permission request dialog shown above will not launch automatically. Developers has to call it manually. In the case that developer try to call some function that requires permission ,which user has not granted yet, the function will suddenly throw an Exception which will lead to the application crashing.


Automatically Granted permissions (Simplified Permissions)

 Traditionally, when developing an Android application, it was required to specify each and every permission needed when calling a specific API. There are some permission that will be automatically granted at install time and will not be able to revoke. We call it Normal Permission(PROTECTION_NORMAL).

Below is the full list of it:

 android.permission.ACCESS_LOCATION_EXTRA_COMMANDS  
 android.permission.ACCESS_NETWORK_STATE  
 android.permission.ACCESS_NOTIFICATION_POLICY  
 android.permission.ACCESS_WIFI_STATE  
 android.permission.ACCESS_WIMAX_STATE  
 android.permission.BLUETOOTH  
 android.permission.BLUETOOTH_ADMIN  
 android.permission.BROADCAST_STICKY  
 android.permission.CHANGE_NETWORK_STATE  
 android.permission.CHANGE_WIFI_MULTICAST_STATE  
 android.permission.CHANGE_WIFI_STATE  
 android.permission.CHANGE_WIMAX_STATE  
 android.permission.DISABLE_KEYGUARD  
 android.permission.EXPAND_STATUS_BAR  
 android.permission.FLASHLIGHT  
 android.permission.GET_ACCOUNTS  
 android.permission.GET_PACKAGE_SIZE  
 android.permission.INTERNET  
 android.permission.KILL_BACKGROUND_PROCESSES  
 android.permission.MODIFY_AUDIO_SETTINGS  
 android.permission.NFC  
 android.permission.READ_SYNC_SETTINGS  
 android.permission.READ_SYNC_STATS  
 android.permission.RECEIVE_BOOT_COMPLETED  
 android.permission.REORDER_TASKS  
 android.permission.REQUEST_INSTALL_PACKAGES  
 android.permission.SET_TIME_ZONE  
 android.permission.SET_WALLPAPER  
 android.permission.SET_WALLPAPER_HINTS  
 android.permission.SUBSCRIBED_FEEDS_READ  
 android.permission.TRANSMIT_IR  
 android.permission.USE_FINGERPRINT  
 android.permission.VIBRATE  
 android.permission.WAKE_LOCK  
 android.permission.WRITE_SYNC_SETTINGS  
 com.android.alarm.permission.SET_ALARM  
 com.android.launcher.permission.INSTALL_SHORTCUT  
 com.android.launcher.permission.UNINSTALL_SHORTCUT  

Just simply declare this permissions in AndroidManifest.xml and it will work just fine. No need to check  for the permissions listed above since it couldn't be revoked.


Getting Started

If there's one way to lessen our worries, its using a library. Like this one. The Permission Helper library will help us implement runtime permissions a whole lot easier !

Start by adding the library to your apps' build.gradle file:

 compile 'com.github.k0shk0sh:PermissionHelper:1.0.7'  


Basic Permssion workflow


            
           It's important to determine which permissions are required and which have been upgraded to the new permission groups, In this  instance, the Geolocator Plugin required both Fine and coarse location permissions, which have been grouped  into android.permission-group.LOCATION. It's still necessary to add these two permissions in the AndroidManifest.xml, but the use  of these permissions is only requested at runtime, not during install, on Android Marshmellow  and above.

 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  


Normal and Dangerous Permissions


That's how Android's permissions are categorized int two groups. The full list is HERE

Cover areas where your app needs to access data or resources outside the app’s sandbox, but where there’s very little risk to the user’s privacy –Normal Permissions”
Dangerous permissions cover areas where the app wants data or resources that involve the user’s private information –Dangerous Permissions
Android will automatically grant access for Normal Permissions. So runtime permissions really come into play for Dangerous Permissions.

Below permissions are grouped into Permission Group like table below:


 If any permission in a Permission Group is granted. Another permission. Another permission in the same group will be automatically granted as well.  In this case, once WRITE_CONTACTS is granted, application will also grant READ_CONTACTS and GET_ACCOUNTS.


Make your application support new Runtime Permission

Let's make our application support new Runtime Permission perfectly. Start with setting compileSdkVersion and targetSdkVersion to 23.


 android {  
      compileSdkVersion 23  
      ...  
      defaultConfig {  
           ...  
           targetSdkVersion 23  
           ...  
      }  

Requesting Runtime permissions

I'm going to request the 'Dangerous' permission of reading the calendar. So for reference sake, I declare it like this.


 final String PERMISSION = Manifest.permission.READ_CALENDAR;  

Implement OnPermissionCallback in your activity. You'll get the following methods:

  1. onPermissionGranted() -permission requested, and successfully granted by user.
  2. onPermissionDeclined() -permission requested, but declined by user.
  3. onPermissionPreGranted() -requested permission is already granted(allowed via app permissions screen)
  4. onPermissionNeedExplanantion() - requested permission was denied, tell user  why you need it.
  5. onPermissionReallyDeclined() - requested permission was denied,and future requests were denied too. (Can only allow from settings now).
  6. onNoPermissionNeeded() - fallback method for the pre Marshmellow devices. (older permission model).

Firstly, Initialize your PermissionHelper: 

 permissionHelper = PermissionHelper.getInstance(this);  

Then request your permission, say in button's OnClickListener:

 permissionHelper.setForceAccepting(false).request(PERMISSION);  

setForceAccepting() helps ensure that the request doesn't force the user to grant permission.

Next, override the Activity's onRequestPermissionResult() method.


 @Override  
 public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {  
 permissionHelper.onRequestPermissionsResult(requestCode, permissions, grantResults);  
 }  


Handling Requested Permission

After requesting permission, we need to handle it:

  1. Permission grant
  2. Permission denial



  1. Permission Grant
            Override the Activity's OnRequestPermissionResult()

 @Override  
 public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {  
 permissionHelper.onRequestPermissionsResult(requestCode, permissions, grantResults);  
 }  

If user granted the permission,the onPermissionGranted() is used,otherwise onPermissionDeclined() is called.

In the user flow diagram ,you can see that denied permission can be requested again. If requested again, this time system dialog doesn't appear. But the permission will be added to your app's Permission screen,from where you can manually enable disable requested permissions.
















So if you want to request denied permission, use the onPermissionNeedExplaination() method. Use this to tell the user WHY you need that permission. I do it with simple dialog explaining why.

 AlertDialog dialog = new AlertDialog.Builder(this)  
 .setTitle(title)  
 .setMessage(message)  
 .setPositiveButton("Request", new DialogInterface.OnClickListener() {  
 @Override  
 public void onClick(DialogInterface dialog, int which) {  
 permissionHelper.requestAfterExplanation(permission);  
 }  
 })  
 .create();  
 dialog.show();  


Finally, If you're requesting for a permission that is already granted,then the onPermissionPreGranted() method comes really handy. When i tried to get this new stuff work in practice, two things worth some elaborations: 

  1. Never ask again
  2. Pre marshmellow handling.

Never asked again


The new permission workflow works like following. Suppose,there is a button that you could press to make a call.
 private static final int PERMISSIONS_REQUEST_CALL_PHONE = 201;  
 private String mManifestPersmission;  
 private int mRequestCode;  
 mManifestPersmission = Manifest.permission.CALL_PHONE;  
 mRequestCode = PERMISSIONS_REQUEST_CALL_PHONE;  
 int permerssion = ActivityCompat.checkSelfPermission(mActivity, mManifestPersmission);           boolean should = ActivityCompat.shouldShowRequestPermissionRationale(mActivity, mManifestPersmission);        
 if (permerssion != PackageManager.PERMISSION_GRANTED) {  
   requestPermission();  
 }  
 private void requestPermission() {  
   ActivityCompat.requestPermissions(mActivity, new String[]{mManifestPersmission}, mRequestCode);  
 }  


And when you press the call button first time, it will show the system dialog first time which i mentioned above.If you choose ALLOW, it would just go and call like pre marshmellow;If you choose DENY, it means you have denied permission for user to access, then better we'd show an alert to guide user what's going like below screenshot:


The idea here is ,so user denied a permission, we need to provide some explanation(i.e. rationale) to tell user why we need it. Also we provide two actions RE-TRY and I'M SURE.  Click Retry, it would prompt permission asking alert again; Click I'm sure, then it just dismiss silently because user has explicitly known what he is doing.

So if you click Retry or you press call button second time,the permission requesting window will have an option: Never ask again


The tricky part here what happens if user denied Never ask again checked?  It turns out in onRequestPermissionResult() , you could query shouldShowRequestPermissionRationale to tell whether user has denied with never ask again.

The code onRequestPermissionResult:
 public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {  
   if(requestCode == mRequestCode){  
     Logger.t(mManifestPersmission);  
     boolean hasSth = grantResults.length > 0;  
     if(hasSth){  
       if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {  
         //user accepted , make call  
         Logger.d("Permission granted");  
         if(this.mAffirmativeCallback != null){  
           this.mAffirmativeCallback.onPermissionConfirmed();  
         }  
       } else if(grantResults[0] == PackageManager.PERMISSION_DENIED) {  
         boolean should = ActivityCompat.shouldShowRequestPermissionRationale(mActivity, mManifestPersmission);  
         if(should){  
           //user denied without Never ask again, just show rationale explanation  
           AlertDialog.Builder builder = new AlertDialog.Builder(mActivity, R.style.AppCompatAlertDialogStyle);  
           builder.setTitle("Permission Denied");  
           builder.setMessage("Without this permission the app is unable to make call.Are you sure you want to deny this permission?");  
           builder.setPositiveButton("I'M SURE", new DialogInterface.OnClickListener() {  
             @Override  
             public void onClick(DialogInterface dialog, int which) {  
               dialog.dismiss();  
             }  
           });  
           builder.setNegativeButton("RE-TRY", new DialogInterface.OnClickListener() {  
             @Override  
             public void onClick(DialogInterface dialog, int which) {  
               dialog.dismiss();  
               requestPermission();  
             }  
           });  
           builder.show();  
         }else{  
           //user has denied with `Never Ask Again`, go to settings  
           promptSettings();  
         }  
       }  
     }  
   }  
 }  
 private void promptSettings() {  
   AlertDialog.Builder builder = new AlertDialog.Builder(mActivity, R.style.AppCompatAlertDialogStyle);  
   builder.setTitle(mDeniedNeverAskTitle);  
   builder.setMessage(mDeniedNeverAskMsg);  
   builder.setPositiveButton("go to Settings", new DialogInterface.OnClickListener() {  
     @Override  
     public void onClick(DialogInterface dialog, int which) {  
       dialog.dismiss();  
       goToSettings();  
     }  
   });  
   builder.setNegativeButton("Cancel", null);  
   builder.show();  
 }  
 private void goToSettings() {  
   Intent myAppSettings = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + mActivity.getPackageName()));  
   myAppSettings.addCategory(Intent.CATEGORY_DEFAULT);  
   myAppSettings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
   mActivity.startActivity(myAppSettings);  
 }  


So if showShowRequestPermissionRationale returns false , we will display an alert for go to app settings to allow user manually toggle permissions.


Last point when requested permission, we need a way to distinguish two case when shouldShowRequestPermissionRationale  returns false. According to android documentation: shouldShowRequestPermissionRationale

To help find the situations where you need to provide extra explanation, the systemprovides the Activity.showShowRequestPermissionRationale(String) method. This method returns true if the app has requested this permission previously and the user denied the request. That indicates that you should probably explain to the user why you need the permission.

If user turned down the permission request in the past and chose the Don't ask again option in the permission request system dialog, this method returns false. The method also returns false if the device policy prohibits the app from having that permission.

Hence we need to modify request code a littel bit: 

 private static final int PERMISSIONS_REQUEST_CALL_PHONE = 201;  
 ...  
 boolean should = ActivityCompat.shouldShowRequestPermissionRationale(mActivity, mManifestPersmission);        
 if (permerssion != PackageManager.PERMISSION_GRANTED) {  
   if (should) {  
     // should show some explanation alert, but here now, just prompt ask again            
     requestPermission();  
   } else {  
     //TWO CASE:  
     //1. first time - system up - //request window  
     if(!PrefUtils.hasLocationPermissionBeenRequested(mActivity)){  
       PrefUtils.markLocationPermissionBeenRequested(mActivity, true);           requestPermission();  
     }else{  
       //2. second time - user denied with never ask - go to settings           promptSettings();  
     }  
   }  
   return;  
 }  


Pre Marshmellow and code reuse

To make it work with per Marshmellow, we could encapsulate those logic in a helper. So in helper,we could specify a general callback for affirmative actions.  On Pre Marshmellow , you could just call that callback; on marshmellow, do permission flow:
 public class PermissionHelper {  
   public interface PermissionAffirmativeCallback  
   {  
     public void onPermissionConfirmed();  
   }  
   private PermissionAffirmativeCallback mAffirmativeCallback;  
   public static PermissionHelper permissionHelper(PermissionType type,  
                             Activity activity,  
                             PermissionAffirmativeCallback callback){  
       return new PermissionHelper(type, activity, callback);  
     }  
   public PermissionHelper(PermissionType type, Activity activity, PermissionAffirmativeCallback callback) {  
     if(type == PermissionType.LOCATION){  
       mManifestPersmission = Manifest.permission.ACCESS_FINE_LOCATION;  
       mRequestCode = PERMISSIONS_REQUEST_LOCATION;  
       mDeniedMsg = "Without this permission the app is unable to find your location.Are you sure you want to deny this permission?";  
       mDeniedNeverAskTitle = "Unable to locate your position";  
       mDeniedNeverAskMsg = "You have denied the permission for location access. Please go to app settings and allow permission";  
     }else if(type == PermissionType.CALL){  
       mManifestPersmission = Manifest.permission.CALL_PHONE;  
       mRequestCode = PERMISSIONS_REQUEST_CALL_PHONE;  
       mDeniedMsg = "Without this permission the app is unable to make call.Are you sure you want to deny this permission?";  
       mDeniedNeverAskTitle = "Unable to make call";  
       mDeniedNeverAskMsg = "You have denied the permission for calling.. Please go to app settings and allow permission";  
     }  
     this.mActivity = activity;  
     this.mAffirmativeCallback = callback;  
     checkPermission();  
   }  
   private void checkPermission() {  
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {  
       int permerssion = ActivityCompat.checkSelfPermission(mActivity, mManifestPersmission);          
       boolean should = ActivityCompat.shouldShowRequestPermissionRationale(mActivity, mManifestPersmission);          
       if (permerssion != PackageManager.PERMISSION_GRANTED) {  
         //...blablabla  
         return;  
       }  
     }  
     if(this.mAffirmativeCallback != null){  
       this.mAffirmativeCallback.onPermissionConfirmed();  
     }  
   }  
   //others  
 }  



Then in activity, you could use like this way: 
 public class MainActivity extends AppCompatActivity {  
   protected List<PermissionHelper> mPermissionHelpers = new ArrayList<>();  
   @Override  
   public void onRequestPermissionsResult(int requestCode,  
                        String permissions[], int[] grantResults) {  
    for(PermissionHelper helper : mPermissionHelpers){  
      helper.onRequestPermissionsResult(requestCode,permissions, grantResults);  
    }  
   }  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     mToolbar = (Toolbar) findViewById(R.id.toolbar);  
     setSupportActionBar(mToolbar);  
     PermissionHelper permissionHelper = PermissionHelper.permissionHelper(PermissionType.LOCATION, this,  
           new PermissionHelper.PermissionAffirmativeCallback() {  
             @Override  
             public void onPermissionConfirmed() {  
               renderMap();  
             }  
           });  
     mPermissionHelpers.add(permissionHelper);  
     permissionHelper = PermissionHelper.permissionHelper(PermissionType.CALL, this,  
     new PermissionHelper.PermissionAffirmativeCallback() {  
       @Override  
       public void onPermissionConfirmed() {  
         makeCall();  
       }  
     });  
     mPermissionHelpers.add(permissionHelper);      
   }  
 }  
Thank you.




Thursday, 3 March 2016

Image Compression without affecting Image quality in Android

Hello Friends,

Today i am going to brief you about how you can upload large size image in compressed form like whatsapp.

The images captured by the recent cameras often exceed 2 MB. Using such images frequently results in OutOfMemoryError. Also, due to landscape/portrait orientation, the images often are displayed as rotated. To deal with these issues we need to compress the image and give proper rotation before loading it to memory. The following method compresses image (similar to whatsapp), while maintaining its aspect ratio and also preventing significant loss to its quality.

I have also implemented demo which contains image selection using camera and gallery also. It reduces size of an image without affecting its quality.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.demo.imagecompressiondemo;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.media.ExifInterface;
import android.os.Environment;
import android.view.inputmethod.InputMethodManager;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by Grishma on 14/3/16.
 */
public class CommonUtils {
    private static final float maxHeight = 1280.0f;
    private static final float maxWidth = 1280.0f;

    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        final float totalPixels = width * height;
        final float totalReqPixelsCap = reqWidth * reqHeight * 2;
        while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
            inSampleSize++;
        }
        return inSampleSize;
    }


    /**
     * Reduces the size of an image without affecting its quality.
     *
     * @param imagePath -Path of an image
     * @return
     */
    public static String compressImage(String imagePath) {
        Bitmap scaledBitmap = null;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        Bitmap bmp = BitmapFactory.decodeFile(imagePath, options);

        int actualHeight = options.outHeight;
        int actualWidth = options.outWidth;

        float imgRatio = (float) actualWidth / (float) actualHeight;
        float maxRatio = maxWidth / maxHeight;

        if (actualHeight > maxHeight || actualWidth > maxWidth) {
            if (imgRatio < maxRatio) {
                imgRatio = maxHeight / actualHeight;
                actualWidth = (int) (imgRatio * actualWidth);
                actualHeight = (int) maxHeight;
            } else if (imgRatio > maxRatio) {
                imgRatio = maxWidth / actualWidth;
                actualHeight = (int) (imgRatio * actualHeight);
                actualWidth = (int) maxWidth;
            } else {
                actualHeight = (int) maxHeight;
                actualWidth = (int) maxWidth;
            }
        }
        options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight);
        options.inJustDecodeBounds = false;
        options.inDither = false;
        options.inPurgeable = true;
        options.inInputShareable = true;
        options.inTempStorage = new byte[16 * 1024];
        try {
            bmp = BitmapFactory.decodeFile(imagePath, options);
        } catch (OutOfMemoryError exception) {
            exception.printStackTrace();
        }
        try {
            scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.RGB_565);
        } catch (OutOfMemoryError exception) {
            exception.printStackTrace();
        }

        float ratioX = actualWidth / (float) options.outWidth;
        float ratioY = actualHeight / (float) options.outHeight;
        float middleX = actualWidth / 2.0f;
        float middleY = actualHeight / 2.0f;
        Matrix scaleMatrix = new Matrix();
        scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
        Canvas canvas = new Canvas(scaledBitmap);
        canvas.setMatrix(scaleMatrix);
        canvas.drawBitmap(bmp, middleX - bmp.getWidth() / 2, middleY - bmp.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));
        if (bmp != null) {
            bmp.recycle();
        }
        ExifInterface exif;
        try {
            exif = new ExifInterface(imagePath);
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
            Matrix matrix = new Matrix();
            if (orientation == 6) {
                matrix.postRotate(90);
            } else if (orientation == 3) {
                matrix.postRotate(180);
            } else if (orientation == 8) {
                matrix.postRotate(270);
            }
            scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        FileOutputStream out = null;
        String filepath = MainActivity.imageFilePath;//getFilename();
        try {
            //new File(imageFilePath).delete();
            out = new FileOutputStream(filepath);

            //write the compressed bitmap at the destination specified by filename.
            scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        return filepath;
    }

    public static String getFilename() {
        File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
                  + "/ImageCompApp/Images");

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            mediaStorageDir.mkdirs();
        }

        String mImageName = "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg";
        String uriString = (mediaStorageDir.getAbsolutePath() + "/" + mImageName);
        return uriString;
    }

    public static void hideKeyboard(Activity context) {
        try {
            if (context == null) return;
            InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(context.getWindow().getCurrentFocus().getWindowToken(), 0);
        } catch (Exception e) {
        }
    }
}
The method getFilename():: It creates a folder in the SDCard used to store the images.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
public static String getFilename() {
        File mediaStorageDir = new File(Environment.getExternalStorageDirectory()
                  + "/ImageCompApp/Images");

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            mediaStorageDir.mkdirs();
        }

        String mImageName = "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg";
        String uriString = (mediaStorageDir.getAbsolutePath() + "/" + mImageName);
        return uriString;
    }
The method calculateInSampleSize:: calculates a proper value for inSampleSize based on the actual and required dimensions:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
 public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height / (float) reqHeight);
            final int widthRatio = Math.round((float) width / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
        }
        final float totalPixels = width * height;
        final float totalReqPixelsCap = reqWidth * reqHeight * 2;
        while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
            inSampleSize++;
        }
        return inSampleSize;
    }
A demo project implementing this compression can be downloaded from Here.


Note:In this approach, the max height and width of the output image is maintained at around 960px and 1280px respectively [float maxHeight = 1280.0f; float maxWidth = 1280.0f;] The final height and width are decided depending upon the aspect ratio(width:height) of the image. The extent of compression required to achieve this ia calculated by the method “calculateInSampleSize”. The method returns an integer value “inSampleSize”. This “inSampleSize” decides the extent of compression. For example if inSampleSize=1, the image will be compressed to half; inSampleSize = 4 means the image the will be compressed to one-fourth. So when we are loading the image from the path, it loads a scaled down version.