OiO.lk Blog Android Error: There was a problem parsing the package
Android

Error: There was a problem parsing the package


As I am trying to download app from apk file from another android app, I am facing this issue.
I have provided all required permission including "Google play protect" permissions.
Here is code

public class MainActivity extends  AppCompatActivity {

    final String tag = "InstallAppMain";
    private ProgressBar progressBar;
    private long downloadID;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button downloadButton = findViewById(R.id.downloadButton);
        Button uninstallButton = findViewById(R.id.uninstallButton);
        progressBar = findViewById(R.id.progressBar);
        progressBar.setVisibility(View.VISIBLE);  // Show progress bar

        registerReceiver(onDownloadComplete,new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

        // Installation permission

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if(!getPackageManager().canRequestPackageInstalls()){
                startActivityForResult(new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES)
                        .setData(Uri.parse(String.format("package:%s", getPackageName()))), 1);
            }
        }
        //Storage Permission

        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE,android.Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
        }

        if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
        }
        downloadButton.setOnClickListener(v -> downloadAndInstallAPK());

        uninstallButton.setOnClickListener(v -> uninstallApp("com.example.apptouninstall"));
    }

    // Download APK and install
    private void downloadAndInstallAPK() {
        checkPermission();

        String apkFilePath = Environment.getExternalStorageDirectory() + "/" + "Download/app-debug.apk";
        File file = new File(apkFilePath);
        if (file.exists()) {
            Toast.makeText(getApplicationContext(), "ّFile found!", Toast.LENGTH_LONG).show();
            Log.i(tag, "File found");

            Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
            String type = "application/vnd.android.package-archive";
            Uri downloadedApk = FileProvider.getUriForFile(getApplicationContext(), "com.example.installapp.provider", file);
            intent.setDataAndType(downloadedApk, type);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
            startActivity(intent);
        }
        else
            Toast.makeText(getApplicationContext(), "ّFile not found!", Toast.LENGTH_LONG).show();
    }


    // Uninstall App
    private void uninstallApp(String packageName) {
        Intent intent = new Intent(Intent.ACTION_DELETE);
        intent.setData(Uri.parse("package:" + packageName));
        startActivity(intent);
    }

    private  void checkPermission() {
        if(Build.VERSION.SDK_INT > Build.VERSION_CODES.M)
        {
            if(checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
                Log.i(tag, "Permission is denied");
                String[] permission = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
                requestPermissions(permission, 10);
            }
            else {
                Log.i(tag, "Permission is granted");
                startDownloadFile();
            }
        }
    }

    private void startDownloadFile() {
        String urlFile = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ForBiggerEscapes.mp4";
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(urlFile));
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);
        request.setTitle("Download");
        request.setDescription("Download file..");

        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "Java_Programming.mp4");

        DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
        if(downloadManager != null) {
            downloadID = downloadManager.enqueue(request);
            Log.i(tag , "Starting downloading" + downloadID);
        }
        else {
            Toast.makeText(getApplicationContext(), "Download", Toast.LENGTH_LONG);
        }
    }



    private BroadcastReceiver onDownloadComplete = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            //Fetching the download id received with the broadcast
            long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
            //Checking if the received broadcast is for our enqueued download by matching download id

                Toast.makeText(MainActivity.this, "Download Completed", Toast.LENGTH_SHORT).show();

        }
    };


    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(onDownloadComplete);
    }
}

Community, I need your guidance,

  1. Why this error raises as I have provided all permissions.
  2. How I could fix this.



You need to sign in to view this answers

Exit mobile version