从相机的图片中恢复真实路径

时间:2018-08-27 16:58:04

标签: android

我正在从相机拍摄照片并将其保存在公共文件夹(Pictures / myFolder)中,并且正在从照片中存储Uri以重新加载到视图中,但是我需要构建一个具有真实路径的文件,但是我无法恢复路径以及他们在互联网上找到的所有代码都给了我空指针,我该如何恢复真实路径?

Uri示例

content://media/content://br.com.technolog.darwinchecklist.fileprovider/darwin_checklist_images/DARWIN_20180827_114154_460340375.jpg/images/media

无效的方法

public String getRealPathFromURI(Uri uri) {
String path = "";
if (getContentResolver() != null) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        path = cursor.getString(idx);
        cursor.close();
    }
}
return path;

}

2 个答案:

答案 0 :(得分:0)

参考:this

new StatusConfig().status(callBack)

答案 1 :(得分:0)

我正在使用以下代码从Uri获取文档的真实路径:

/**
 * Retrieve filename from Uri
 *
 * @param uri             Uri following the schemes: "file" or "content"
 * @param contentResolver ContentResolver to resolve content scheme
 *
 * @return filename if operation succeded. Can be null.
 */
public static String getFileName(@NonNull final Uri uri, @NonNull final ContentResolver contentResolver) {
    String filename = "";
    if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
        filename = uri.getLastPathSegment();
    } else if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
        try {
            Cursor cursor = contentResolver.query(uri, null, null, null, null);
            int index = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
            cursor.moveToFirst();
            filename = cursor.getString(index);
            cursor.close();
        } catch (Exception e) {
            Log.e(TAG, "Exception when retrieving file name: " + e.getMessage());
        }
    }

    return filename;
}