Android 如何在高于24的目标SDK中打开apk文件?
我有一个下载apk文件的应用程序。在我的应用程序中,我下载了一个apk文件,并希望在下载完成后安装这个apk文件。我用api 19在手机上测试了它,它可以正常工作。但它在牛轧糖版本上不起作用。如何更改此代码才能在牛轧糖及更高版本上真正起作用 安装apk的api低于24的代码是:Android 如何在高于24的目标SDK中打开apk文件?,android,file,android-intent,android-contentprovider,android-install-apk,Android,File,Android Intent,Android Contentprovider,Android Install Apk,我有一个下载apk文件的应用程序。在我的应用程序中,我下载了一个apk文件,并希望在下载完成后安装这个apk文件。我用api 19在手机上测试了它,它可以正常工作。但它在牛轧糖版本上不起作用。如何更改此代码才能在牛轧糖及更高版本上真正起作用 安装apk的api低于24的代码是: Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/icm/" + fileName)), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startActivity(intent);
不适用于牛轧糖及更高版本的代码:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri uri = FileProvider.getUriForFile(getContext(), getContext().getApplicationContext().getPackageName() + "clicksite.org.cafebazar.fileprovider", new File(Environment.getExternalStorageDirectory() + "/icm/" + fileName));
intent.setDataAndType(uri, "application/pdf");
getActivity().startActivity(intent);
android清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="clicksite.org.cafebazar">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<category android:name="android.intent.category.LAUNCHER"/>
<action android:name="android.intent.action.MAIN"/>
</intent-filter>
</activity>
<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>
</application>
以及res文件夹中xml目录中的provider_path.xml:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
请帮助我如何更改此代码以适用于牛轧糖版本及更高版本问题1:权限错误
这不匹配:
android:authorities="${applicationId}.provider"
将前面的行更改为:
Uri uri = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".provider", new File(Environment.getExternalStorageDirectory(), "icm/" + fileName));
问题2:MIME类型错误
APK文件不是PDF文件。将此更改为:
intent.setDataAndType(uri, "application/vnd.android.package-archive");
问题可能不止这两个,因为你没有解释你的症状是什么。但是,这些问题肯定需要解决。我还建议使用API级别14或更高的版本,而不是
ACTION\u VIEW
intent.setDataAndType(uri, "application/pdf");
intent.setDataAndType(uri, "application/vnd.android.package-archive");