0

Currently, I need to update an installed application offline. To update the application, I have placed an apk file to the downloads folder of file manager and by pressing a button click event I want to update the apk but failed and facing an issue of "There was a problem while parsing the package error". I have rooted phone so is there any adb command or anything is available? I have also added exported="true" in manifest file and set allow to unknown permission for the app. Any help will be appreciable.

private void installApk() {
    try {
        String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/Demo.apk";
        File file = new File(path);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT >= 24) {
            Uri downloaded_apk = FileProvider.getUriForFile(MainActivity.this, getApplicationContext().getPackageName() + ".provider", file);
            intent.setDataAndType(downloaded_apk, "application/vnd.android.package-archive");
            List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
            for (ResolveInfo resolveInfo : resInfoList) {
                grantUriPermission(getApplicationContext().getPackageName() + ".provider", downloaded_apk, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
            }
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
//                startActivity(intent);
        } else {
            intent.setAction(Intent.ACTION_VIEW);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        startActivity(intent);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

minsdk is 19 and maxsdk is 33

Nikhil
  • 1,212
  • 13
  • 30
  • Check with file.exists() and file.canRead() before building an intent. – blackapps Nov 08 '22 at 20:26
  • the value of file.exists() is true but the value of file.canRead() is false. I have already added the permission of Manage_External_Storage Permission. What should I do? @blackapps – Nikhil Nov 09 '22 at 11:08
  • On Android 11+ devices apps have no read or write access to files they did not create. So if you placed the file there no access. If you complete the MANAGE_EXTERNAL_STORAGE to the end then your app can read the file. Just mentioning in manifest file is not enough. – blackapps Nov 09 '22 at 11:50
  • Is there any way to detect the apk file? My app is admin app and also the device is rooted. So is there any solution to use PackageInstaller for it? @blackapps – Nikhil Nov 09 '22 at 11:52
  • You can select the file with ACTION_OPEN_DOCUMENT. – blackapps Nov 09 '22 at 11:54

0 Answers0