I'm selecting an image to use as User's picture in my app like this:
Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, IMAGE_GALLERY);
In onActivityResult()
if(requestCode == IMAGE_GALLERY && resultCode == RESULT_OK) {
Uri uri = intent.getData();
if(uri != null) {
this.picture = Utils.ScaleBitmap(context, uri, 640);
userPic.setScaleType(ImageView.ScaleType.CENTER_CROP);
userPic.setPadding(0,0,0,0);
userPic.setImageBitmap(picture);
}
}
Where my Utils.ScaleBitmap method is the following:
try {
//Getting file path from URI
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(imageURI, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
//Getting EXIF info to rotate image
ExifInterface exif = null;
try {
File pictureFile = new File(picturePath);
exif = new ExifInterface(pictureFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
int orientation = ExifInterface.ORIENTATION_NORMAL;
if (exif != null)
orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
bitmap = rotateBitmap(bitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
bitmap = rotateBitmap(bitmap, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
bitmap = rotateBitmap(bitmap, 270);
break;
}
//Compressing image
int w = bitmap.getWidth(), h = bitmap.getHeight();
int width, height;
if (w > max || h > max) {
if (w == h) {
width = height = max;
} else if (w < h) {
height = max;
width = max * w / h;
} else {
width = max;
height = max * h / w;
}
} else {
width = w;
height = h;
}
//Bitmap bitmap = ((BitmapDrawable) commentImage.getDrawable()).getBitmap();
Bitmap scaledphoto = Bitmap.createScaledBitmap(bitmap, width, height, true);
return scaledphoto;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
The problem is that, this code doesn't work with images that I pick from the cloud, like Google Drive, Picasa, etc.
It used to work when I didn't do all the rotating stuff. It was only
Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), imageURI);
And I could pick any image and worked. But the images from my camera were getting a wrong orientation. Now I corrected the rotation, but cannot get images from cloud.
Does anyone know how could I get both things working?
I mean, I want to be able to select images from Cloud Storage, and also be able to rotate the images that have wrong orientation.