Intention to install APK file - Android Nougat

I searched a lot and did not find a problem. When you try to install the APK file using Intent in Android Nougat , it simply does not install and displays the following warning: "There was a problem parsing the package."

It works great to open PDF files, for example, with the settings for opening this type of file (. PDF ). However, for installation. apk files do not work.

LogCat does not show any errors and I cannot find any solution.

What could be wrong?

The following code:

manifest:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INSTALL_PACKAGES" /> <provider android:name="android.support.v4.content.FileProvider" android:authorities="br.com.xxxxxx.xxxxxx.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths"/> </provider> 

XML / File Paths:

 <paths xmlns:android="http://schemas.android.com/apk/res/android"> <files-path name="storage/emulated/0" path="."/> 

Activity:

 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { try { Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE); File file = new File(getContext().getFilesDir(), "app-debug.apk"); Uri uri = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".fileprovider", file); intent.setDataAndType(uri, "application/vnd.android.package-archive"); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); startActivity(intent); finish(); } catch (Exception e) { e.printStackTrace(); } }else{ try { Intent intent = new Intent(Intent.ACTION_VIEW); File file = new File(Environment.getExternalStorageDirectory() + "/app-debug.apk"); intent.setAction(android.content.Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); startActivity(intent); finish(); } catch (Exception e) { e.getMessage(); } } 

Please, what could be wrong with this code? Can anybody help me?

+5
source share
1 answer

I had to change the intent for N (and above) and remove the type designation. As soon as I did this, the installation worked as expected.

So for N:

 Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE); File file = new File(getContext().getFilesDir(), "app-debug.apk"); Uri uri = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".fileprovider", file); intent.setData(uri) intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); startActivity(intent); finish(); 
+1
source

Source: https://habr.com/ru/post/1262272/


All Articles