当前位置:网站首页>Android 72021 latest Android interview written test questions sharing

Android 72021 latest Android interview written test questions sharing

2022-06-24 00:07:00 Ordinary netizens

android.os.FileUriExposedException: file:///storage/emulated/0/Download/xxxAppName.apk exposed beyond app through Intent.getData()

The code called by the mobile terminal is as follows :

Intent intentUpdate = new Intent(“android.intent.action.VIEW”);
intentUpdate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri apkUri = Uri.fromFile(new File(upgradeMsg.apkpath));
intentUpdate.setDataAndType(apkUri, “application/vnd.android.package-archive”);
startActivity(intentUpdate);

This error is mainly reported as google stay 6.0 Later versions have made permission restrictions , Applications Not directly through **file://Uri Share resources with another , Need to pass through content://Uris ** To share resources , So that the platform can extend the temporary permission of the receiving application to access resources . however N The previous version can still pass file:// To share resources .
The main reasons are summarized as follows :

  1. Suppose the shared file is private , receive file://Uri Of App Unable to access file
  2. stay Android6.0 After that, the runtime permissions are introduced , If file://Uri Of app No application Manifest.permission.READ_EXTERNAL_STORAGE jurisdiction , When the file is read Will cause a crash

The solution to this mistake google The plan is also given , use FileProvider, It is a file establish content://Uri instead of file://Uri To share files securely .
FileProvider Main steps :

  1. Definition FileProvider
  2. Specify available files
  3. To retrieve the contents of a file URI
  4. to grant authorization URI Temporary authority
  5. Providing content to another application URI

stay AndroidManifest.xml Add the following code to



Be careful : authorities:app The package name .fileProvider grantUriPermissions: Must be true, Indicates grant URI Temporary access rights exported: Must be false resource: Medium @xml/file_paths It's the file we're going to add next

stay res Create a new one in the directory xml Folder , And build a new file_paths Of xml file

path: Path requiring temporary authorization to access (. For all paths ) name: It's you who give this access path a name

It's almost time to modify the installation code , The code is as follows :

private void installApk() { // Erection sequence
Intent intentUpdate = new Intent(“android.intent.action.VIEW”);
intentUpdate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // Yes Android N And above
Uri apkUriN = FileProvider.getUriForFile(MainActivity2.this,
MainActivity2.this.getApplicationContext().getPackageName() + “.FileProvider”, new File(upgradeMsg.apkpath));
intentUpdate.addCategory(“android.intent.category.DEFAULT”);
intentUpdate.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // Day off. Flag Indicates what permissions we need
intentUpdate.setDataAndType(apkUriN, “application/vnd.android.package-archive”);
} else {
Uri apkUri = Uri.fromFile(new File(upgradeMsg.apkpath));
intentUpdate.setDataAndType(apkUri, “application/vnd.android.package-archive”);
}
startActivity(intentUpdate);
}

The code is written , Do you think it's ok ? When you are happy in 6.0 perhaps 7.0 ~ 8.0 When updating and installing , There is nothing wrong with it , But when you are 8.0 When installing versions and above , Click Update It will flash by , Or there is an error parsing the package . Why? ?
Because in Aandroid 8.0 When Google Some restrictions have been made

Android 8.0 Oreo in ,Google Removed easily abused “ Allow location source ” Switch for application , In the installation Play Store Third party sources other than Android When applied , It's gone “ Allow unknown sources ” Check box for , If you still want to install a developer you trust app, You need to grant... Manually every time “ Install unknown app ” Permission of .

We have to fit 8.0 And above .
The first thing you need to do is AandroidManifest.xml Add permission in

Secondly, when clicking update, you need to judge the mobile phone version information :

private static final

《Android Summary of learning notes + Latest mobile architecture video + Big Android interview questions + Project actual combat source code handout 》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 Full content open source sharing

int INSTALL_PACKAGES_REQUESTCODE = 10011;
private static final int GET_UNKNOWN_APP_SOURCES = 10012;
private void checkAndroidO() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // System Android O And above
// Need to handle unknown application source permissions . true Install the package for user trust false You need to obtain authorization
boolean canRequestPackageInstalls = getPackageManager().canRequestPackageInstalls();
if (canRequestPackageInstalls) {
installApk();

原网站

版权声明
本文为[Ordinary netizens]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206232147194547.html