Skip to content Skip to sidebar Skip to footer

Image Path From Uri Returns Null

I'm trying to get the path of the selected file from gallery, but it is returning null and I don't know why. Every code I see uses the same approach, but it doesn't work for me. He

Solution 1:

Did you add the following line in your manifest ? =]

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Solution 2:

Could you explain a little more what your problem is? Where are you getting a null?

In which Android version are you running this code? From Android 4.4 onwards, the filechooser which opens when you send the intent for picking up an image returns a relative uri, since it shows not only the files that are stored in your device but also the ones stored in the cloud. So, it could be happening that you're getting a relative URI and when you query for it's location on the device you're getting null, since the ContentResolver doesn't have the path of that file.

If that's the case (Actually even if you're not, since you should develop your app with compatibility for Android's new versions) i'd recommend you to use Content Resvolver to open a InputStream to get the file (openInputStream(Uri), since it will allow you to fetch a file from any location (both local and cloud).

I hope it helps :)

Solution 3:

Well, here is how i do in my live wallpaper (Noiraude, have a look :P )

@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode,
            Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

        switch (requestCode) {
        case100:
            if (resultCode == RESULT_OK) {
                UriselectedImage= imageReturnedIntent.getData();
                @SuppressWarnings("unused")InputStreamimageStream=null;
                try {
                    imageStream = getContentResolver().openInputStream(
                            selectedImage);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                sharedPreferences = getSharedPreferences("NLP_settings", 0);
                Editoreditor= sharedPreferences.edit();
                editor.putString("key_bit", getPath(selectedImage));
                editor.commit();
                restartThis();
            }
        }
    }

    public String getPath(Uri uri) {
        // just some safety built inif (uri == null) {
            returnnull;
        }
        // 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 };
        Cursorcursor= managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            intcolumn_index= cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        // this is our fallback herereturn uri.getPath();
    }

Post a Comment for "Image Path From Uri Returns Null"