I have an updated app in my device storage. I have to install that upper version app from my existing lower version app in a button click. (Like the way dora tv users update their app) I wrote this code.But it's showing - "There was a problem parsing the package". There is no error in logcat & no exception. What i need to do now? App Install error
@RequiresApi(api = Build.VERSION_CODES.M)
private void requestPermission() {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_PERMISSION);
}
private void havePermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
&& !getPackageManager().canRequestPackageInstalls()) {
Intent unknownAppSourceIntent = new Intent()
.setAction(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES)
.setData(Uri.parse(String.format("package:%s", getPackageName())));
startActivityForResult(unknownAppSourceIntent, Constants.APP_INSTALL_REQUEST);
//unknownAppSourceDialog.launch(unknownAppSourceIntent);
} else {
apkInstall();
}
}
private void apkInstall() {
String path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS).getPath() + File.separator + "test.apk";
Log.d("filePath", path);
File file = new File(path);
if (file.exists() && !file.isDirectory()) {
// do something
Log.d("fileExist", file.getName());
}
Intent intent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri apkUri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".provider", file);
intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setData(apkUri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
Uri apkUri = Uri.fromFile(file);
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
try {
startActivityForResult(intent, Constants.APP_INSTALL_REQUEST);
} catch (Exception e) {
e.printStackTrace();
BizUiUtils.showToast(this, "Error in installing App, please try again");
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constants.APP_INSTALL_REQUEST) {
apkInstall();
}
}