1

I am trying to install an apk programmatically.

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/AppFolder/" + "app.apk")), "application/vnd.android.package-archive");
startActivity(intent);

This is the code I have used. But when I run the code it prompts to choose package installer and when I select package installer I am getting the error "There was a problem while parsing the package".

This is my MainActivity.

public class MainActivity extends AppCompatActivity {

private Button btnInstall;
private String path;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if(Build.VERSION.SDK_INT>=24){
        try{
            Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
            m.invoke(null);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    btnInstall = (Button) findViewById(R.id.btn);
    btnInstall.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory()  + "Snapseed.apk")), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    });
}
}

Am trying to install Snapseed app from the SDcard by clicking the button`

Thanks in advance.

Solved : Thank you all.I have found the issue. I was saving the file in my External storage but in the code, I was pointing to internal storage.

Ram Koti
  • 2,203
  • 7
  • 26
  • 36
Eby Cloudins
  • 111
  • 8

3 Answers3

1

Add Flag in your code. Try this one. It works for me.

 Intent intent = new Intent(Intent.ACTION_VIEW);
 intent.setDataAndType(Uri.fromFile(new File
 (Environment.getExternalStorageDirectory() + "/download/" + "Xender.apk")),
 "application/vnd.android.package-archive");
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(intent);
0

I was also getting the same error in my case.

String path = Environment.getExternalStorageDirectory().getAbsolutePath() +"/app-debug.apk";

intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");

We've to check the label of existing and the new .apk should be same.

Hope this will help.

Edit

    Intent i = new Intent();
    i.setAction(Intent.ACTION_VIEW);
    i.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive" );
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    Log.d("Lofting", "About to install new .apk");
    startActivity(i);

The above code is working fine for me, I can install or update my existing app.

Shambhu
  • 181
  • 6
  • 16
  • labels are same – Eby Cloudins Feb 19 '18 at 09:19
  • I think, you should look into the path. try adding `getAbsolutePath()`. Please check, the apk name, the default is `app-debug.apk` or `app-release.apk` for the signed one. Is this `Snapseed.apk` after generating the .apk? – Shambhu Feb 19 '18 at 13:49
  • i don't think, it need here. I was getting `parsing error` because of name of `apk`. I had changed the app name as `Lavazza.apk` and the app was generated as `app-debug.apk`. Is these same in your case? – Shambhu Feb 20 '18 at 11:52
  • First i renamed it. But now am using it as app-debug.apk – Eby Cloudins Feb 20 '18 at 11:55
  • Well, this is the issue. Try uninstalling the previous one. Install the app without changing the name and then use the program of installation. – Shambhu Feb 20 '18 at 12:48
  • The app is not installed now, i need to install it only using program – Eby Cloudins Feb 21 '18 at 04:17
  • now am using the app-debug apk. I have not renamed it – Eby Cloudins Feb 21 '18 at 04:18
  • Even then it's getting the name `Snapseed.apk`, I believe. First time try installing manually with the label `app-debug.apk`. And then the program will work. – Shambhu Feb 21 '18 at 05:40
  • I am able to install it maually. But not using program – Eby Cloudins Feb 21 '18 at 06:17
  • Have you tried manually after changing the name from `Snapseed` to `app-debug` ? – Shambhu Feb 21 '18 at 06:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165535/discussion-between-eby-cloudins-and-shambhu). – Eby Cloudins Feb 21 '18 at 06:21
0

Try this one,

add permission on Manifest file

  <uses-permission android:name="android.permission.INSTALL_PACKAGES" />

and try this code for installing apk

Intent newIntent = new Intent();
newIntent.putExtra(PackageUtil.INTENT_ATTR_APPLICATION_INFO, 
mPkgInfo.applicationInfo);
newIntent.setData(mPackageURI);
newIntent.setClass(this, InstallAppProgress.class);
String installerPackageName = 
getIntent().getStringExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME);
if (installerPackageName != null) {
    newIntent.putExtra(Intent.EXTRA_INSTALLER_PACKAGE_NAME, 
    installerPackageName);
}
startActivity(newIntent);
Saif
  • 723
  • 6
  • 21