I am writing a code that allows to download an application from server and open it automatically , but im facing an exception after calling the Intent (install) in the last step of the code
Exception :
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///file:/storage/emulated/0/Download/application.apk typ=application/apk }
Please know that file in question is an application called application.apk and I would like to open the install page directly after the download finish . Here is the code bellow :
private void download(String url) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); //Notify client once download is completed!
final String filename= URLUtil.guessFileName(url, null, null);
final String file_extension = filename.replace("bin","apk");
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, file_extension);
final DownloadManager dm = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
final Long enq= dm.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading File", //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();
/// OPEN File
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(enq);
Cursor c = dm.query(query);
if (c.moveToFirst()) {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
//TODO : Use this local uri and launch intent to open file
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(Uri.fromFile(new File(uriString)), "application/apk");
webview_java.this.startActivity(install);
}
}
}
}
};
registerReceiver(receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
I've also tried startActivity instead of calling webview_java.this.startActivity but it throws the same error