You can Call this function like,
Uri selectedImage = data.getData();
Bitmap b = decodeUri(selectedImage);
decodeUri function is Here, this will decode the Uri and will return the Bitmap.
private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
Bitmap resizedBitmap = null;
errSmallImage = false;
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream( getContentResolver().openInputStream(selectedImage), null, o);
final int REQUIRED_SIZE = 100;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
if(width_tmp >=300 || height_tmp >=300 ){
System.out.println("decodeUri : Original Resolution : , "+width_tmp+"x"+height_tmp);
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
//return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);
Bitmap b = BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);
Matrix matrix = new Matrix();
float rotation = rotationForImage(context, selectedImage);
if (rotation != 0f) {
matrix.preRotate(rotation);
}
resizedBitmap = Bitmap.createBitmap(b, 0, 0, width_tmp, height_tmp, matrix, true);
}else{
errSmallImage=true;
resizedBitmap = null;
}
return resizedBitmap;
}
public static float rotationForImage(Context context, Uri uri) {
if (uri.getScheme().equals("content")) {
String[] projection = { Images.ImageColumns.ORIENTATION };
Cursor c = context.getContentResolver().query(
uri, projection, null, null, null);
if (c.moveToFirst()) {
return c.getInt(0);
}
} else if (uri.getScheme().equals("file")) {
try {
ExifInterface exif = new ExifInterface(uri.getPath());
int rotation = (int)exifOrientationToDegrees(
exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL));
return rotation;
} catch (IOException e) {
Log.e("Photo Import", "Error checking exif", e);
}
}
return 0f;
}
private static float exifOrientationToDegrees(int exifOrientation) {
if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) {
return 90;
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) {
return 180;
} else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
return 270;
}
return 0;
}