Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

retrieve absolute path when select image from gallery kitkat android

Tags:

As I am supporting my app to Kitkat version, now in this the way of retrieve file from gallery was different.

I have preferred this Android Gallery on KitKat returns different Uri for Intent.ACTION_GET_CONTENT for retrieving file from gallery and successfully work but I required Absolute path of that file, I am getting

content://com.android.providers.media.documents/document/image:2505 

For 19 below version we used uri different by using that I am getting path this way

Cursor cursor = this.getContentResolver().query(originalUri, projection, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); String fpath = cursor.getString(column_index); 

but in 19 version it will give me null value how to get absolute path of image file which was selected by user.

Thanks

like image 831
Pratik Avatar asked Nov 28 '13 07:11

Pratik


1 Answers

Here is one way to access the Absolute path after selecting file.

After getting data in new URI format for KK(KitKat) like this way

content://com.android.providers.media.documents/document/image:2505 

Just extract ID of your document

if(requestCode == GALLERY_KITKAT_INTENT_CALLED && resultCode == RESULT_OK){      Uri originalUri = data.getData();      final int takeFlags = data.getFlags()                         & (Intent.FLAG_GRANT_READ_URI_PERMISSION                         | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);     // Check for the freshest data.     getContentResolver().takePersistableUriPermission(originalUri, takeFlags);      /* now extract ID from Uri path using getLastPathSegment() and then split with ":"     then call get Uri to for Internal storage or External storage for media I have used getUri()     */      String id = originalUri.getLastPathSegment().split(":")[1];      final String[] imageColumns = {MediaStore.Images.Media.DATA };     final String imageOrderBy = null;      Uri uri = getUri();     String selectedImagePath = "path";      Cursor imageCursor = managedQuery(uri, imageColumns,           MediaStore.Images.Media._ID + "="+id, null, imageOrderBy);      if (imageCursor.moveToFirst()) {         selectedImagePath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));     }     Log.e("path",selectedImagePath ); // use selectedImagePath  }else if() {       // for older version use existing code here }  // By using this method get the Uri of Internal/External Storage for Media private Uri getUri() {     String state = Environment.getExternalStorageState();     if(!state.equalsIgnoreCase(Environment.MEDIA_MOUNTED))         return MediaStore.Images.Media.INTERNAL_CONTENT_URI;      return MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } 
like image 54
Pratik Avatar answered Oct 15 '22 04:10

Pratik