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;
}

相关内容

热门资讯

突发!hhpoker透视脚本下... 突发!hhpoker透视脚本下载,微信微乐辅助器使用教程,分享透视(确实有挂)-哔哩哔哩1)hhpo...
透视总结!aapoker脚本,... 您好,这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】很多玩家在这款游戏中打...
突发!wepoker辅助器最新... 突发!wepoker辅助器最新版本更新内容,微信微乐自建房辅助工具,揭幕透视(详细教程)-哔哩哔哩1...
透视总结!hhpoker视频巡... 透视总结!hhpoker视频巡查真的假的,hh poker辅助器先试用(透视)确实有神器(哔哩哔哩)...
今年以来!epoker免费透视... 今年以来!epoker免费透视脚本,微乐小程序脚本入口,开挂脚本(揭秘有挂)-哔哩哔哩1、epoke...
透视解迷!hhpoker辅助器... 透视解迷!hhpoker辅助器,hhpoker透视脚本(透视)确实存在有神器(哔哩哔哩)1、让任何用...
为了进一步!pokerrrr2... 您好,pokerrrr2辅助这款游戏可以开挂的,确实是有挂的,需要了解加去威信【485275054】...
脚本辅助!aapoker免费透... 脚本辅助!aapoker免费透视脚本,aapoker怎么设置抽水(透视)都是真的是有方法(哔哩哔哩)...
此事迅速冲上热搜!hh pok... 此事迅速冲上热搜!hh poker软件,微信微乐辅助器免费安装,解谜透视(新版有挂)-哔哩哔哩1、h...
脚本专业!德普之星透视辅助软件... 脚本专业!德普之星透视辅助软件,德普之星的辅助工具介绍(透视)总是有方法(哔哩哔哩)1、实时透视辅助...