I've fetched a user selected image from gallery and want to display it in a imageview
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), SELECT_PICTURE);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
imageView.setImageBitmap(BitmapFactory
.decodeFile(selectedImagePath));
Log.i("SELECTED IMAGE: ", selectedImagePath);
}
}
}
/**
* helper to retrieve the path of an image URI
*/
public String getPath(Uri uri) {
// just some safety built in
if( uri == null ) {
// TODO perform some logging or show user feedback
return null;
}
// try to retrieve the image from the media store first
// this will only work for images selected from gallery
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if( cursor != null ){
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
// for other file managers
return uri.getPath();
}
There are some images with path as: /storage/sdcard0/WhatsApp/Media/WhatsApp Images/IMG-20150407-WA0013.jpg which get displayed properly
And there are some images which give path as a google download link. Example: https://lh3.googleusercontent.com/-9VYhxxxmZs/VdQnxxxo5I/AAAAxxxCpM/4B3s-xxoeI/I/2015-08-08.jpg (path changed in the post for privacy)
These images do not get displayed.
I one such case,
I'm getting Uri as content://com.sec.android.gallery3d.provider/picasa/item/5913341278276321746
and selectedImagePath as https://lh3.googleusercontent.com/-9VYhxxxmZs/VdQnxxxo5I/AAAAxxxCpM/4B3s-xxoeI/I/2015-08-08.jpg
How can I get selectedImagePath of such images as a path in the phone and not as a web location?