0

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

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Chaouki Anass
  • 937
  • 1
  • 10
  • 19
  • 1
    You want to write an app that downloads an `.apk` from the server, installs it and run it? Don't think that's possible without root permission. – Krzysztof Kubicki Apr 17 '18 at 15:49
  • @KrzysztofKubicki I know that but Im not asking to install the app in the background , I just want to open the install page for the user and he will have the choice to install it or not – Chaouki Anass Apr 17 '18 at 15:51
  • Download + Open file in generally doesnt request a root – Chaouki Anass Apr 17 '18 at 15:51
  • 1
    Maybe this: https://stackoverflow.com/questions/4604239/install-application-programmatically-on-android –  Apr 17 '18 at 15:51
  • What @Andy posted looks promising. From my side I can only say that if you target a specific type of device (like samsung) you can use their SDK to perform such task. I think it's paid but maybe your project can afford it. Google Samsung Knox for reference. Good luck! – Krzysztof Kubicki Apr 17 '18 at 15:54
  • `"application/apk"` That is the wrong mimetype. Google for the rigth one. – greenapps Apr 17 '18 at 16:26

0 Answers0