0

I am trying to install a non-market app in Android. It is downloaded in: file:///mnt/sdcard/Download/App.apk

What I am doing is:

Intent promptInstall = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(uriString)).setType("application/vnd.android.package-archive");
startActivity(promptInstall);

where uriString is : file:///mnt/sdcard/Download/App.apk.

But there is an exception:

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW typ=application/vnd.android.package-archive }

EDIT

I am downloading the app using DownloadManager:

DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request req = new DownloadManager.Request(Uri.parse(MY_PATH_TP_APK));
Request req.setTitle("Test")
       .setDescription("Something useful.")
       .setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "App.apk");
long enqueue = dm.enqueue(req);

After downloading:

_receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
            Query query = new Query();
            query.setFilterById(enqueue);
            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));
                    Intent promptInstall = new  Intent(Intent.ACTION_VIEW)
                    .setData(Uri.parse(uriString))
                    .setType("application/vnd.android.package-archive");
                    startActivity(promptInstall);
                }
            }
        }
    }
};

How to fix it?

jnthnjns
  • 8,962
  • 4
  • 42
  • 65
b.i
  • 1,087
  • 4
  • 22
  • 43
  • Possible duplicate of [Android: install .apk programmatically](http://stackoverflow.com/questions/4967669/android-install-apk-programmatically) – jk2K Jan 22 '16 at 07:46

2 Answers2

3

I'm using this code in one of my apps and it's working fine:

Uri fileUri = Uri.fromFile(new File("/mnt/sdcard/Download", "App.apk"));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(fileUri, "application/vnd.android.package-archive");
startActivity(intent);

Maybe your code has one slash too much (file:///mnt instead of file://mnt (not sure here)).

Tim
  • 6,692
  • 2
  • 25
  • 30
  • Are you using a stock Android or is it a Samsung, HTC or whatever phone with a custom surface? Maybe they've changed the package installer's Intent? – Tim Jun 21 '12 at 06:54
  • I am using Sony ericsson, xperia arc with android 2.3.4 – b.i Jun 21 '12 at 06:56
  • I am downloading the app using the DownloadManager class and trying to install it in the onReceive method. I will edit my code. – b.i Jun 21 '12 at 06:59
1

replace

String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));

with:

c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME));
Spudley
  • 166,037
  • 39
  • 233
  • 307
Hoang An
  • 11
  • 1