简体   繁体   中英

How to get the path of image android?

I am selecting an image of the gallery from my app, using

ACTION_GET_CONTENT

Now I want the path of this image:

在此处输入图片说明

But i get it like this:

content://com.android.providers.media.documents/document/image%3A5793

How can i get the path?

I tried this to get path:

Uri uri = data.getData();
Log.e("Path is: "+uri);

The answer is don't try and get the path because in Android 10 and later you won't be able to get it or use it.

You can get a FileDescriptor or input/output Stream that can be used in most methods that need to access the file contents.

See https://developer.android.com/training/data-storage/ for more details, since it is a picture then using Media store is probably best.

Try this :

Uri uri = data.getData();
        String picturePath = getPath( getActivity( ).getApplicationContext( ), uri);
        Log.e("Picture Path", picturePath);

public static String getPath( Context context, Uri uri ) {
    String result = null;
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.getContentResolver( ).query( uri, proj, null, null, null );
    if(cursor != null){
        if ( cursor.moveToFirst( ) ) {
            int column_index = cursor.getColumnIndexOrThrow( proj[0] );
            result = cursor.getString( column_index );
        }
        cursor.close( );
    }
    if(result == null) {
        result = "Not found";
    }
    return result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM