简体   繁体   中英

get real path from URI of type data.getData() file:///storage/emulated/0/LenovoReaper/did

My application going to ANR when I choose file and try to get its real path. my

 public static String getRealPathFromUri(Context context, Uri contentUri) {
        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();
            }
        }
    }

but same thing works fine if I choose any media file like image.

Works fine for data.getData() = content://media/external/images/media/22555 real path

real path is /storage/emulated/0/DCIM/20150406_210053.jpg

for non media file it goes to ANR

data.getData() file:///storage/emulated/0/LenovoReaper/did

nothing is returned and Application goes to ANR . Could some one suggest how to get real path of such URI or should I just remove file:// from the data.getData()

For opening the gallery use this code:

Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction("android.intent.action.PICK");
                        startActivityForResult(intent, PICK_IMAGE_REQUEST);

In onActivityResult :

if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
                && data != null && data.getData() != null) {  
   String absolutePath = "file://" + getPath(this,data.getData());  
            
        }

And the getPath function:

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";
    }
    Log.d(TAG, "onActivityResult: file://"+result);
    return result;
}

It will give you the absolute path of the image like "file:///storage/emulated/0/LenovoReaper/did"

Could some one suggest how to get real path of such URI

There is no "real path" other than the Uri itself.

should I just remove file:// from the data.getData()

That will not do you any good.

A Uri is not a File . There is no requirement for any Uri to have to point to a file that you can access. If you want to get access to the data represented by the Uri , use methods on ContentResolver , such as openInputStream() .

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