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

相关内容

热门资讯

透视了解!pokemmo内置修... 透视了解!pokemmo内置修改器,大菠萝789辅助器下载-果然是真的有辅助教程(哔哩哔哩)1、这是...
透视分享!wpk辅助器,wpk... 透视分享!wpk辅助器,wpk辅助器-其实有辅助软件(哔哩哔哩)1、玩家可以在线上大神俱乐部对游戏的...
透视关于!德普之星透视,德普之... 透视关于!德普之星透视,德普之星透视软件免费入口官网-好像是有辅助app(哔哩哔哩)1、起透看视 辅...
透视开挂!xpoker辅助助手... 透视开挂!xpoker辅助助手,pokemmo脚本辅助下载-原来一直总是有辅助神器(哔哩哔哩)1、下...
透视解谜!wpk俱乐部怎么作弊... 透视解谜!wpk俱乐部怎么作弊,wpk官网下载链接-好像一直都是有辅助插件(哔哩哔哩)1、该软件可以...
透视详细!aapoker透视脚... 透视详细!aapoker透视脚本下载,aapoker透视脚本下载-都是有辅助方法(哔哩哔哩)1、上手...
透视关于!wepoker透视有... 透视关于!wepoker透视有没有,wepoker透视脚本下载-好像真的是有辅助方法(哔哩哔哩)1、...
透视有挂!wepoker手机插... 透视有挂!wepoker手机插件,wejoker开挂-竟然是有辅助app(哔哩哔哩)1、首先打开辅助...
透视解迷!aapoker ai... 透视解迷!aapoker ai插件,aapoker辅助怎么用-本来是真的有辅助神器(哔哩哔哩)1、游...
透视必备!aapoker安装包... 透视必备!aapoker安装包怎么使用,aapoker脚本怎么用-确实真的有辅助方法(哔哩哔哩)1、...