2

I've successfully downloaded my apk file to internal storage.

However when running it I get an "No activity found to handle intent" error.

Here is my code to run the apk file.

// Old version
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.N)
{
File apkFile = new File(this.pathToFile);
     if(apkFile.exists())
     {
          Uri apkURI = Uri.fromFile(apkFile);
          Intent intent = new Intent(Intent.ACTION_VIEW);
          intent.setData(apkURI);
          intent.setType("application/vnd.android.package-archive");
          intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, false);
          intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          context.startActivity(intent);
    }
}
mocode10
  • 589
  • 1
  • 6
  • 23
  • `my file path is actually /data/com.me.appName/files/update.apk`. That is an impossible path. Only on a rooted device it could have been created. – greenapps Nov 17 '17 at 09:24
  • `successfully downloaded my apk file to internal storage.`. Well how did you manage to do that? Considering that impossible path. – greenapps Nov 17 '17 at 09:25
  • You were talking about `this.pathToFile`? – greenapps Nov 17 '17 at 09:27
  • `Android how to install apk stored in internal storage`. Wrong subject. – greenapps Nov 17 '17 at 09:29
  • context.openFileOutput("app.apk", Context.MODE_PRIVATE); Is where it was saved. Then I stored pathToFile = context.getFilesDir().toString() + "/app.apk"; – mocode10 Nov 17 '17 at 17:53
  • Then your path and pathToFile is /data/data/com.me.appName/files/update.apk – greenapps Nov 17 '17 at 18:20
  • You're right. I must have messed up last night when typing this up. However I've updated the question. I added a check to make sure the file exists before launching the intent and it turns out that returns false. I included my code to download the file in my post. Can you tell me what's going wrong? Possibly wrong path? – mocode10 Nov 17 '17 at 18:30
  • Please add the check if that file exist to the code. – greenapps Nov 17 '17 at 18:32
  • Your code looks ok. You are shure there is no excepion? How do you check? Also catch IOException. – greenapps Nov 17 '17 at 18:35
  • `getFilesDir().toString() `. I would use getFilesDir().getAbsolutePath() instead. – greenapps Nov 17 '17 at 18:36
  • Gotcha I just changed that and updated the original post. When it launches the intent I get an error parsing package error and the app crashes. – mocode10 Nov 17 '17 at 19:16
  • I believe that there is a parsing error while the app gets installed by Android. But that your app would crash is strange. What is the Android version of device and app? – greenapps Nov 17 '17 at 19:28
  • The app downloading the apk and the apk being downloaded target 4.0. The android version is 6.0. – mocode10 Nov 17 '17 at 19:38
  • I updated the post. Got download and constructing the URI just fine now. No longer target new os version that requires FileProvider. But even on old version I'm still having an issue with no activity found to handle intent. – mocode10 Nov 17 '17 at 19:44

1 Answers1

0

The apk cannot be installed using a content scheme.

You have to offer Android a file path to the apk file.

So do away with the FileProvider.

Further you should download to getExternalStorageDirectory() or getFilesDir() instead.

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • I do download to getFilesDir(). File apkFile = new File(context.getFilesDir().getAbsolutePath() + "/app.apk"); – mocode10 Nov 17 '17 at 19:48
  • Yes i know. So dont do that was what i said. – greenapps Nov 17 '17 at 19:48
  • Is there any reason to choose ExternalStorage over getFilesDir()? My reason for choosing getFilesDir is because every device has it where not every device has external storage media. – mocode10 Nov 17 '17 at 19:50
  • Every device has external storage. And a lot of devices have also removeble storage if a micro SD card can be inserted. Do not use getFilesDir() as it s private internal storage for your app only. So other apps have no acces. Im afraid that your apk cannot be installed from internal storage. Well if Android installs the apk then it should have acces. But please try both. Why not? – greenapps Nov 17 '17 at 19:56
  • The apk is not the app that tries to instal it? Or is it? – greenapps Nov 17 '17 at 19:58
  • I have app1 which is where all this code is. And it is trying to install app2 (The apk downloaded). I am going to change my code to use external storage now and see what happens. – mocode10 Nov 17 '17 at 20:02
  • Changed it to File apkFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/app-release.apk"); But I'm still getting the no activity found error. – mocode10 Nov 17 '17 at 20:23
  • You removed the download code. But if you stil use openFileOutput() you are still saving to getFilesDir(). Use FileOutputStream instead with a full parh. Not only a file name as you do now. – greenapps Nov 17 '17 at 21:23
  • Or did you change it? You still have the exists check before the intent,? Better shiw full code. – greenapps Nov 17 '17 at 21:25
  • I scratched my approach entirely and used https://stackoverflow.com/a/4969421/7129932 I did change it from openFileOutput but still had no luck with installation for whatever reason. I appreciate your time spent trying to help me, you're a good person. – mocode10 Nov 17 '17 at 21:32