I searched extensively and did not find the problem. When trying to install an APK file using a Intent
in Android Nougat
, it simply does not install and displays the following warning: "There was a problem parsing the package."
It works perfectly to open PDF files, for example, with adjustments to open this type of file (% with%). But to install. APK does not work.
No .PDF
does not show any errors and I can not reach any solution.
What could be wrong?
Follow the code below:
Manifest :
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="br.com.xxxxxxx.xxxxxxx.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths"/>
</provider>
xml / filepaths:
<files-path name="storage/emulated/0" path="."/>
Code:
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 anyone help me?