【发布时间】:2016-11-01 06:24:58
【问题描述】:
我有以下设置,但是在第三方应用程序中打开文件时,找不到该文件。
下载文件(以字节为单位)后,我将其存储。
public File storeFile(byte[] b, String ref) throws IOException {
String path = String.valueOf(ref).replaceAll("/+", "_");
final File f = new File(A.getDir(path, Context.MODE_PRIVATE), "downloads");
FileOutputStream out = new FileOutputStream(f);
out.write(b);
out.flush();
out.close();
return f;
}
然后尝试阅读并打开它。
try {
Uri uri = Uri.fromFile(storeFile(bytes, ref));
MimeTypeMap mime = MimeTypeMap.getSingleton();
Intent i = new Intent(Intent.ACTION_VIEW);
String type = mime.getMimeTypeFromExtension(ext);
File imagePath = new File(getFilesDir(), "downloads");
File file = new File(imagePath, uri.getPath());
Uri newUri = FileProvider.getUriForFile(context, "com.do.app.fileprovider", file);
i.setDataAndType(uriContent, type);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
A.startActivity(i);
} catch (IOException e) {
e.printStackTrace();
}
在清单中
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.do.app.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
路径
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path name="cloud_files" path="downloads"/>
</paths>
我似乎找不到我缺少的东西。
【问题讨论】:
-
FileProvider.getUriForFile()。它恢复正常吗?请告诉uri.toString()的值。
标签: java android android-fileprovider