我想让我的应用程序自动安装apk。这适用于api <24。但对于24岁,它失败了。 Android实现了额外的安全性:
对于定位到Android 7.0的应用,Android框架会强制执行StrictMode API策略,禁止在应用外部公开file:// URI。如果包含文件URI的intent离开了您的应用程序,则该应用程序将失败,并显示FileUriExposedException异常。
所以我尝试了这个:
Uri myuri;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N){
myuri = Uri.parse("file://"+outapk);
} else {
File o = new File(outapk);
myuri = FileProvider.getUriForFile(con, con.getApplicationContext().getPackageName() + ".provider", o);
}
Intent promptInstall = new Intent(Intent.ACTION_VIEW).setDataAndType(myuri,"application/vnd.android.package-archive");
con.startActivity(promptInstall);
但得到致命的例外:
com.android.packageinstaller "Caused by: java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord{b42ee8a 6826:com.android.packageinstaller/u0a15} (pid=6826, uid=10015) that is not exported from uid 10066".
我的清单中有export = true。
问题似乎是packageinstaller无法使用content:// uri。
有没有办法允许应用程序以api24的方式逐步安装apk?
答案 0 :(得分:12)
有没有办法允许应用程序以api24的方式逐步安装apk?
将addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
添加到promptInstall
设置,以授予对内容的读取权限。
我的清单中有export = true。
不在FileProvider
上,因为这会导致您的应用崩溃。
问题似乎是packageinstaller无法使用content:// uri。
不,问题是您没有授予程序包安装程序读取Uri
的权限。如果软件包安装程序无法使用content
方案,那么您将获得ActivityNotFoundException
。
但请注意,只有Android 7.0才能使软件包安装程序开始支持content
。早期版本的Android必须使用file
。
答案 1 :(得分:4)
对于奥利奥(Oreo),在AndroidManifast中添加权限(否则,它会自动失败)
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
现在添加到您的清单
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
在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>
然后在需要的地方使用这些代码。
File directory = Environment.getExternalStoragePublicDirectory("myapp_folder");
File file = new File(directory, "myapp.apk"); // assume refers to "sdcard/myapp_folder/myapp.apk"
Uri fileUri = Uri.fromFile(file); //for Build.VERSION.SDK_INT <= 24
if (Build.VERSION.SDK_INT >= 24) {
fileUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
}
Intent intent = new Intent(Intent.ACTION_VIEW, fileUri);
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
intent.setDataAndType(fileUri, "application/vnd.android.package-archive");
intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //dont forget add this line
context.startActivity(intent);
}
答案 2 :(得分:2)
对于奥利奥, 在AndroidManifast中添加权限
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
答案 3 :(得分:0)
在res/xml
-> provider_paths.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>
将此代码添加到AndroidManifest.xml
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.provider" <-- change this with your package name
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
运行以下代码以安装您的应用或打开
public void installApk(String file) {
Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider",new File(file));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent);
}
答案 4 :(得分:0)
只需执行以下步骤:
1-添加以下权限以显示:
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
2-将提供程序添加到清单(作为子级作为应用程序标记):
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="tatcomputer.ir.libraryapp.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/paths"/>
</provider>
3-将path.xml添加到xml目录:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="files" path="." />
<external-files-path name="external_files" path="." />
<external-path name="external_files" path="." />
<cache-path name="cached_files" path="." />
<external-cache-path name="cached_files" path="." />
</paths>
4-使用以下代码显示安装apk页面(请注意,在我的情况下,apk位于名为tmp.apk的手机的根目录中:
String root = Environment.getExternalStorageDirectory().toString();
Uri fileUri24 = FileProvider.getUriForFile(App.applicationContext, BuildConfig.APPLICATION_ID + ".provider", new File(root + "/tmp.apk"));
Uri fileUri = Uri.fromFile(new File(root + "/tmp.apk"));
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= 24)
intent.setDataAndType(fileUri24, "application/vnd.android.package-archive");
else intent.setDataAndType(fileUri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
App.applicationContext.startActivity(intent);
答案 5 :(得分:0)
这是我找到的解决方案
val newFile = File(dirPath, "$fileNameWithoutExtn.apk")
var fileUri = Uri.fromFile(newFile)
//use the fileProvider to get the downloaded from sdcard
if (Build.VERSION.SDK_INT >= 24) {
fileUri = FileProvider.getUriForFile(this@SettingAcitivity, applicationContext.packageName + ".provider", newFile)
val intent=Intent(Intent.ACTION_VIEW)
intent.setDataAndType(fileUri, "application/vnd.android.package-archive")
intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivity(intent)
}else{
newFile.setReadable(true, false)
val intent = Intent(Intent.ACTION_VIEW)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
intent.setDataAndType(fileUri, "application/vnd.android.package-archive")
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivity(intent)
}
并写入清单
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/paths"/>
并设置权限
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
,并且在xml文件夹中的路径将是
<paths>
<external-path
name="external_files"
path="." />
</paths>
答案 6 :(得分:0)
自动安装Apk
Intent intent1 = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent1.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent1.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" +filename)), "application/vnd.android.package-archive");
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
答案 7 :(得分:0)
以我为例,在android 8.0上的问题是
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
更详细的说明如何获取此权限Exception 'open failed: EACCES (Permission denied)' on Android