Android 5.1.1 lollipop return null file path if image chosen from gallery -


android 5.1.1 lollipop return null file path if image chosen gallery. below code works fine in devices below 5.1.1, doesn't work in lollipop 5.1.1

uri contenturi = data.getdata(); cursor cursor = null;     try {         string[] proj = {mediastore.images.media.data};         cursor = context.getcontentresolver().query(contenturi, proj, null, null, null);         int column_index = cursor.getcolumnindexorthrow(mediastore.images.media.data);         cursor.movetofirst();         return cursor.getstring(column_index);     } {         if (cursor != null) {             cursor.close();         }     } 

cursor.getstring(column_index) returns null.

for have ended getting image gallery. i've tested on 4.4, 5.0.1 , 5.1.1 should work on previous versions (with new , old google photo app), should less hacky , doesn't require check on android version.

public static uri handleimageuri(uri uri) {     if (uri.getpath().contains("content")) {         pattern pattern = pattern.compile("(content://media/.*\\d)");         matcher matcher = pattern.matcher(uri.getpath());         if (matcher.find())             return uri.parse(matcher.group(1));         else             throw new illegalargumentexception("cannot handle uri");     }     return uri; } 

and used same code have ever used before getting image path:

public static string getrealpathfromuri(context context, uri uri) {     cursor cursor = null;     try {         uri newuri = handleimageuri(uri);         string[] proj = { mediastore.images.media.data };         cursor = context.getcontentresolver().query(newuri,  proj, null, null, null);         int column_index = cursor.getcolumnindexorthrow(mediastore.images.media.data);         cursor.movetofirst();         return cursor.getstring(column_index);     } catch (exception e){         return null;     } {         if (cursor != null) {             cursor.close();         }     } } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -