Android10中,从图片URI获取文件路径无法真正实现。
创始人
2024-08-20 09:00:57
0

在Android 10及以上版本中,无法直接从图片URI获取其真实的文件路径。可以使用以下代码示例中的方法,将图片文件先复制到应用程序专有目录的缓存目录中,然后再操作该文件。

private String getFilePathFromUri(Context context, Uri uri) {
    String fileName = getFileName(uri);
    File cacheDir = context.getCacheDir();
    File file = new File(cacheDir, fileName);

    try (InputStream inputStream = context.getContentResolver().openInputStream(uri);
         OutputStream outputStream = new FileOutputStream(file)) {

        byte[] buffer = new byte[4 * 1024];
        int read;
        while ((read = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, read);
        }
        outputStream.flush();

    } catch (IOException e) {
        e.printStackTrace();
    }

    return file.getAbsolutePath();
}

private String getFileName(Uri uri) {
    String fileName = "";
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    if (cursor != null && cursor.moveToFirst()) {
        fileName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
        cursor.close();
    }
    return fileName;
}

相关内容

热门资讯

透视必备!约局吧德州透视(透视... 透视必备!约局吧德州透视(透视)淘宝买wepoker透视有用吗,教程教程书(有挂讲解)-哔哩哔哩1、...
透视关于!德普之星辅助器app... 透视关于!德普之星辅助器app(透视)pokermaster脚本,教程方式(有挂细节)-哔哩哔哩暗藏...
透视总结!佛手在线大菠萝智能辅... 透视总结!佛手在线大菠萝智能辅助器(透视)aa poker辅助,教程手段(有挂细节)-哔哩哔哩1、游...
透视必备!hhpoker俱乐部... 透视必备!hhpoker俱乐部是干嘛的(透视)哈糖大菠萝助手,教程手筋(有挂方法)-哔哩哔哩暗藏猫腻...
透视曝光!wepoker黑侠破... 透视曝光!wepoker黑侠破解(透视)wejoker私人辅助软件,教程模板(讲解有挂)-哔哩哔哩1...
透视解迷!菠萝辅助器免费版的功... 透视解迷!菠萝辅助器免费版的功能介绍(透视)wepoker轻量版书签,教程法子(确实有挂)-哔哩哔哩...
透视揭幕!wepoker私人局... 透视揭幕!wepoker私人局透视方法(透视)hh poker辅助有用吗,教程步骤(有挂头条)-哔哩...
透视必备!wepoker怎么设... 透视必备!wepoker怎么设置房间(透视)wepokerplus开挂,教程方案(有挂解惑)-哔哩哔...
透视开挂!wepoker有辅助... 透视开挂!wepoker有辅助工具吗(透视)wpk有那种辅助吗,教程总结(有挂方针)-哔哩哔哩1.w...
透视专业!hhpoker有辅助... 透视专业!hhpoker有辅助吗(透视)pokemmo辅助器脚本下载,教程讲义(有挂猫腻)-哔哩哔哩...