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();
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.