0

My function for downloading apk and installing it is this, but even though the apk is downloaded successfully and saved, I get an error during installation.

        String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
    String fileName = "App.apk";
    destination += fileName;
    final Uri uri = Uri.parse("file://" + destination);
    File file = new File(destination);
    if (file.exists())
        file.delete();
    if (!file.canRead()) {
        Log.d("installPackage", "File can't be read!");
    }
    //set downloadmanager
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(linkdownload));
    request.setVisibleInDownloadsUi(true);
    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
    request.setDescription("در حال دانلود بروزرسانی");
    request.setTitle("App");
    request.setDestinationUri(uri);
    final DownloadManager manager = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);
    final long downloadId = manager.enqueue(request);
    Intent intent = new Intent(Intent.ACTION_VIEW);

    intent.setDataAndType(uri, manager.getMimeTypeForDownloadedFile(downloadId));
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    this.startActivity(intent);
    finish();

enter image description here

My function for downloading apk and installing it is this, but even though the apk is downloaded successfully and saved, I get an error during installation.

  • Try this solution https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed – Ammar Abdullah Nov 02 '22 at 10:13
  • Beyond the duplicate, please bear in mind that the file is not yet downloaded when the call to `enqueue()` returns. It will not even have *started* downloading. Hence, you cannot try to install it yet. – CommonsWare Nov 02 '22 at 11:48

0 Answers0