android:获取fileprovider生成的文件frmuri时出现问题

rvpgvaaj  于 2021-06-26  发布在  Java
关注(0)|答案(0)|浏览(186)

我像这样在外部存储器中创建一个文件
我的文件名是 (UP)VwJp5A9ASZFTmcIxVf+Yqg==.jpg ```
public static File createFileInExternalStorage(Context context, String filename, byte[] content) throws IOException{
// this is my external file dir path
// /storage/emulated/0/Android/data/{my package}/files
File file = new File(context.getExternalFilesDir(null), filename);
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
fos.write(content);
fos.close();
return file;
}

然后使用文件提供程序将其转换为uri

File file = createFileInExternalStorage(context, filename, data);
String authority = context.getApplicationContext().getPackageName() + ".provider";
Uri processedUri = FileProvider.getUriForFile(context, authority, file);

但是我无法将这个uri返回到原始文件

String path = uri.getPath();
// path is -> /name2/(UP)VwJp5A9ASZFTmcIxVf+Yqg==.jpg
File fdelete = new File(path);
!fdelete.exists() // false

我的 `provider_paths.xml` ```
<?xml version="1.0" encoding="utf-8"?>
<paths>
    <files-path name="name1" path="."/>
    <external-files-path name="name2" path="." />
</paths>

名字2来自 uri.getPath() 肯定是从我的名字来的 external-files-path 有人能解释一下这个名字是什么吗?它在内存中创建了一个名为myimages的文件夹?如果我只想将文件直接保存在内部存储文件夹(顶层)中呢?它强迫我创建一个子文件夹?

取自https://developer.android.com/training/secure-file-sharing/setup-sharing

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题