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 from gallery. The below code works fine in all the devices below 5.1.1, but 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);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

cursor.getString(column_index) this returns null.

  • Problem saving shared preferences in Android
  • Android google maps marker disable navigation option
  • Widget not deleted when passing RESULT_CANCELED as result for configuration activity
  • hardcoded string “row three”, should use @string resource
  • How do I pass parameters to android Intent in new scheme on chrome?
  • Android MVP: safe use Context in Presenter
  • Related posts:

    Build failed after updating tools for Android L
    Rotate byte array of JPEG after onPictureTaken
    Duplicate AndroidManifest.xml in bin directory?
    Android OpenGL Texture Compression
    View.inflate vs LayoutInflater
    Android OpenGL: crazy aspect ratio after sleep
  • How To Change TextView Text on DataChange Without Calling Back TextWatcher Listener
  • Android - build separate APKs for different processor architectures
  • Develop with LibVLC for android on Windows
  • How to trigger broadcast receiver when gps is turn on/off?
  • Android - Proguard with httpcore and httpmime using Android Studio and Gradle
  • Spanning columns with TableLayout
  • 2 Solutions collect form web for “Android 5.1.1 lollipop return null file path if image chosen from gallery”

    For now I have ended up with this for getting an image from gallery. I’ve tested it on 4.4, 5.0.1 and 5.1.1 but it should work on previous versions too (with new and old Google photo app), should be less hacky and doesn’t require a 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 this URI");
        }
        return uri;
    }
    

    And with this I used the same code I have ever used before for getting the 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;
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    

    For a temporary hack-around for android lollipop 5.1.1. It Works fine now. But m not satisfied with this unofficial hack.

    Uri selectedImage = data.getData();
            if (Build.VERSION.SDK_INT == 22) {
                if (selectedImage != null && selectedImage.toString().length() > 0) {
                    try {
                        final String extractUriFrom = selectedImage.toString();
                        String firstExtraction = extractUriFrom.contains("com.google.android.apps.photos.contentprovider") ? extractUriFrom.split("/1/")[1] : extractUriFrom;
                        firstExtraction = firstExtraction.contains("/ACTUAL") ? firstExtraction.replace("/ACTUAL", "").toString() : firstExtraction;
    
                        String secondExtraction = URLDecoder.decode(firstExtraction, "UTF-8");
                        selectedImage = Uri.parse(secondExtraction);
                    } catch (UnsupportedEncodingException e) {
    
                    } catch (Exception e) {
    
                    }
                }
            }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.