最近遇到两个情况,
情况一:android8.0以上,targetSdkVersion=26,程序升级下载之后,就直接闪退;
修改: android8.0以上,targetSdkVersion=22,程序升级下载之后,可以直接安装
情况二:android 10,targetSdkVersion=22,程序升级下载之后,就直接闪退;
所以结合以上的两种情况, android8.0以上,targetSdkVersion=22无法解决android10升级问题,所以才在去研究的。
之前下载完apk后,下载apk的DownloadService.class里面升级的代码如下:
private File outputFile;
outputFile = new File(Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_DOWNLOADS), "file.apk");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setDataAndType(Uri.fromFile(outputFile), "application/vnd.android.package-archive");
startActivity(intent);
解决办法:
第一步,在AndroidManifest.xml中
1、增加权限:
<!--自动安装apk-->
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<!--允许装载和卸载文件系统权限-->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
2、增加provider,如下代码:(com.zoesoft.zymobilenurse是项目的包名)
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<application
...>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.zoesoft.zymobilenurse.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
...
</application>
</manifest>
第二步,指定想分享的目录。在res目录下新建一个xml目录,在xml目录下面新建一个xml文件。我新建的文件名叫filepaths.xml(升级apk我这边是下载在Download文件夹里面)
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<!--代表外部存储区域的根目录下的文件 Environment.getExternalStorageDirectory()/Downloads目录-->
<external-path name="Download" path="Download" />
</paths>
第三步,下载apk的DownloadService.class修改
// private File outputFile; // 存放apk的file
// outputFile = new File(Environment.getExternalStoragePublicDirectory
// (Environment.DIRECTORY_DOWNLOADS), "file.apk");
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= 24) {
Uri apkUri = FileProvider.getUriForFile(this, "com.zoesoft.zymobilenurse.fileprovider", outputFile);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
} else {
intent.setDataAndType(Uri.parse("file://" + outputFile.toString()), "application/vnd.android.package-archive");
}
startActivity(intent);
android 9以上,启动相机报错
public static void startCamera(Activity activity) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//适配7.0拍照取uri的处理
if(Build.VERSION.SDK_INT < 24){
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
Environment.getExternalStorageDirectory(), TMP_PATH)));
}else{
Uri uri= FileProvider.getUriForFile(activity,"com.zoesoft.zymobilenurse.fileprovider",new File(
Environment.getExternalStorageDirectory(), TMP_PATH));
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION );//添加这一句表示对目标应用临时授权该Uri所代表的文件
}
activity.startActivityForResult(intent, CAMERA_WITH_DATA);
}
注意:
遇到的报错
参考:Attempt to invoke virtual method 'android.content.res.XmlResourceParser
有问题欢迎指出。
参考链接:
FileProvider 的使用(Failed to find configured root that contains/storage/emulated/0/DCIM/ )
java.lang.IllegalArgumentException: Failed to find configured root that contains