java.io.FileNotFoundException in android

回眸只為那壹抹淺笑 提交于 2019-12-04 05:46:05

Call this method, pass it the selectedUri, in return you will get the path that you want

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

Just an update since managedQuery has been deprecated.

private String getPath(Uri uri) {
    String[]  data = { MediaStore.Images.Media.DATA };
    CursorLoader loader = new CursorLoader(context, uri, data, null, null, null);
    Cursor cursor = loader.loadInBackground();
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}
Budius

that's because content:/media/external/images/media/526 is not a file. It's an URI address of the media provider. If you want the file check this SO question:

Get filename and path from URI from mediastore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!