Android 从SD卡安装APK(FileProvider出现问题)

Android 从SD卡安装APK(FileProvider出现问题),android,apk,android-fileprovider,Android,Apk,Android Fileprovider,我与文件提供商有问题。我已成功地将apk下载到手机上。apk存储在SD或内部电话存储器中 (SD Card) /storage/3565-6665/Android/data/com.mytest/files/My App Name/Download/app-v1.3.apk (internal storage) /storage/emulated/0/Android/data/com.mytest/files/My App Name/Download/app-v1.3.apk 问题是,从内部

我与文件提供商有问题。我已成功地将apk下载到手机上。apk存储在SD或内部电话存储器中

(SD Card)
/storage/3565-6665/Android/data/com.mytest/files/My App Name/Download/app-v1.3.apk

(internal storage)
/storage/emulated/0/Android/data/com.mytest/files/My App Name/Download/app-v1.3.apk
问题是,从内部存储中可以找到并安装该文件,但在SD卡上该文件不工作,并引发错误异常:

找不到包含的已配置根目录 /存储/3565-6665/Android/data/com.mytest/files/myapp Name/Download/app-v1.3.apk

My path.xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="." /></paths>
我将以以下方式安装apk:

File file = my_file;
Uri fileUri = Uri.fromFile(file); //for Build.VERSION.SDK_INT <= 24
if (Build.VERSION.SDK_INT >= 24) {
    fileUri = FileProvider.getUriForFile(getApplication(), BuildConfig.APPLICATION_ID + ".provider", file);
}
Intent promptInstall = new Intent(Intent.ACTION_VIEW, fileUri);
promptInstall.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
promptInstall.setDataAndType(fileUri, "application/vnd.android.package-archive");
promptInstall.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK);
promptInstall.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(promptInstall);
File File=my_文件;
urifileuri=Uri.fromFile(文件)//对于Build.VERSION.SDK(INT=24){
fileUri=FileProvider.getUriForFile(getApplication(),BuildConfig.APPLICATION_ID+“.provider”,文件);
}
Intent promptInstall=新的Intent(Intent.ACTION\u视图,fileUri);
PrompInstall.putExtra(Intent.EXTRA\u非未知\u来源,true);
setDataAndType(fileUri,“application/vnd.android.package归档”);
prompInstall.setFlags(意图、标志、活动、新任务);
addFlags(Intent.FLAG\u GRANT\u READ\u URI\u权限);
星触觉(PrompInstall);

文件提供程序
不支持可移动存储。要么:

  • 始终下载到您所谓的“内部存储”(但从Android SDK的角度来看是)

  • 让用户使用
    ACTION\u OPEN\u文档
    选择可移动存储上的APK,并使用从中获得的
    Uri
    查看
    操作
    意图

  • 创建您自己的
    ContentProvider
    ,可以为应用程序可移动存储目录中的文件提供服务,然后将该
    ContentProvider
    而不是
    FileProvider
    用于您的
    Uri

  • 切换到使用
    PackageInstaller
    ,因为这绕过了对任何类型的
    ContentProvider
    的需要,并解决了在Android 10上不推荐使用
    操作视图安装应用程序的问题+


我认为使用PackageInstaller可能是最好的解决方案。你能提供一个我如何在代码中使用它的例子吗?“我已经搜索过了,但还不太清楚。”机械:(包含在)演示了基本步骤。
File file = my_file;
Uri fileUri = Uri.fromFile(file); //for Build.VERSION.SDK_INT <= 24
if (Build.VERSION.SDK_INT >= 24) {
    fileUri = FileProvider.getUriForFile(getApplication(), BuildConfig.APPLICATION_ID + ".provider", file);
}
Intent promptInstall = new Intent(Intent.ACTION_VIEW, fileUri);
promptInstall.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
promptInstall.setDataAndType(fileUri, "application/vnd.android.package-archive");
promptInstall.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK);
promptInstall.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(promptInstall);