【发布时间】:2018-02-18 02:06:27
【问题描述】:
我知道 SO 有很多关于这个问题的问题。我尝试了大约 10 种不同的方法,但都无法正常工作。我总是得到下一个错误:
java.lang.IllegalArgumentException: Failed to find configured root that contains /file:/storage/emulated/0/Android/data/app.kwork/files/IMG-afbfdc57c016fb1ff7dd983a056edffa-V.jpg
Official tutorial 也没有帮助。我将在下面分享我的代码,也许我做错了什么,你知道这里到底出了什么问题..
清单:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="app.kwork.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
provider_paths.xml
<paths>
<files-path path="." name="files" />
</paths>
我将文件保存到:
File futureStudioIconFile = new File(getExternalFilesDir(null) + File.separator + fileName);
文件路径示例:file:///storage/emulated/0/Android/data/app.kwork/files/IMG-afbfdc57c016fb1ff7dd983a056edffa-V.jpg
并尝试与下一个代码共享此文件:
public void openFile(String filePath){
File file = new File(filePath);
Log.d(TAG, "openFile: filePath: " + filePath);
Uri photoURI = FileProvider.getUriForFile(context, "app.kwork.provider", file);
MimeTypeMap myMime = MimeTypeMap.getSingleton();
Intent newIntent = new Intent(Intent.ACTION_VIEW);
String mimeType = myMime.getMimeTypeFromExtension(Utils.fileExt(filePath));
newIntent.setDataAndType(photoURI,mimeType);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
newIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
context.startActivity(newIntent);
} catch (ActivityNotFoundException e) {
Toast.makeText(context, "No handler for this type of file.", Toast.LENGTH_LONG).show();
}
}
我在 xml 中尝试了不同的权限和路径,但我现在在这里。
UPD:在 Android Oreo 上测试
【问题讨论】:
标签: android android-fileprovider