lucasdc
lucasdc

Reputation: 1042

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. Here is my code:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // LOAD_FILE_REQUEST is a global variable: 
    // private static final int LOAD_FILE_REQUEST = 1;

    if (requestCode == LOAD_FILE_REQUEST && resultCode == RESULT_OK && data != null) {
        if(data.getData() == null) {
            System.out.println("NULL");
        } else { 
            System.out.println("NOT NULL"); // <--- Printed
        }

        currImageURI = data.getData();  

        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(currImageURI, filePathColumn, null, null, null); 
        if(cursor.moveToFirst()){
           int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
           String yourRealPath = cursor.getString(columnIndex);
           System.out.println("REAL PATH "+yourRealPath);
        } else {
            System.out.println("NO ROWS!!!"); // <-- Not printed
        }
        cursor.close();
    }

} 

Upvotes: 1

Views: 1893

Answers (3)

Laluka
Laluka

Reputation: 61

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


@Override
    protected void onActivityResult(int requestCode, int resultCode,
            Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

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

    public String getPath(Uri uri) {
        // just some safety built in
        if (uri == null) {
            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);
        }
        // this is our fallback here
        return uri.getPath();
    }

Upvotes: 2

Laluka
Laluka

Reputation: 61

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

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

Upvotes: 3

Jonatan Collard Bovy
Jonatan Collard Bovy

Reputation: 558

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 :)

Upvotes: 2

Related Questions