我发送带有Intent的文件的代码不适用于所有文件源,我找不到解决方案:
我的应用已注册打开文件,因此当我在ES文件资源管理器中选择文件时,意图如下:
intent->mData->uriString=/storage/emulated/0/backups/apps/NextGenEndPoint_1.0.apk
在Asus File Manager中选择同一个文件时,我得到以下意图:
intent->mData->uriString=/file/sdcard/backups/apps/NextGenEndPoint_1.0.apk
这是我的代码:
// With this path it works. Path example being sent by e.g ES File Explorer
String fileName = "/storage/emulated/0/backups/apps/PDFViewer.apk";
// with this path it doesn't. This path is sent by Asus File Explorer
fileName = "/file/sdcard/backups/apps/PDFViewer.apk";
final Intent installIntent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
installIntent.setData(getUriForFile(context, "com.myapp.fileprovider", new File(fileName)));
installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(installIntent);
我还尝试了新文件(“/ file / sdcard / backups / apps / PDFViewer.apk”)。exists();什么返回false。
xml:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external" path="/"/>
</paths>
清单中的提供者:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.myapp.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths" />
</provider>
例外:
11-02 12:21:09.004: E/ACRA(9807): com.mycompany.myapp fatal error : Failed to find configured root that contains /file/sdcard/PDFViewer.apk
11-02 12:21:09.004: E/ACRA(9807): java.lang.IllegalArgumentException: Failed to find configured root that contains /file/sdcard/PDFViewer.apk
11-02 12:21:09.004: E/ACRA(9807): at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(SourceFile:711)
11-02 12:21:09.004: E/ACRA(9807): at android.support.v4.content.FileProvider.getUriForFile(SourceFile:400)
我错过了什么?
答案 0 :(得分:1)
在引用的文件中:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external" path="/"/>
</paths>
使用files-path而不是external-path
或
使用Environment.getExternalStorageDirectory()
答案 1 :(得分:0)
/file/sdcard/backups/apps/NextGenEndPoint_1.0.apk.
不,这只是你得到的内容的一部分。来自华硕的文件管理器应用程序将提供
content://com.asus.filemanager.OpenFileProvider/file/sdcard/backups/apps/NextGeEndPoint_1.0.apk.
您最好在确定“路径”的位置显示您的代码。就像现在你做错了。
答案 2 :(得分:0)
问题是将getUriForFile用于文件系统路径以及实际必须与内容解析器一起使用的文件提供程序路径,因此解决方案如下:
Uri uri;
if (ContentResolver.SCHEME_CONTENT.equals(fileUri.getScheme())) {
uri = new Uri.Builder()
.path(fileUri.getPath())
.authority(fileUri.getAuthority())
.scheme(ContentResolver.SCHEME_CONTENT)
.build();
} else {
uri = getUriForFile(context, "com.myapp.fileprovider", new File(fileUri.getPath()));
}