Eby Cloudins
Eby Cloudins

Reputation: 111

Installing apk programatically causing parse error

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.

Upvotes: 1

Views: 595

Answers (3)

Shambhu
Shambhu

Reputation: 191

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.

Upvotes: 0

Darshini Trivedi
Darshini Trivedi

Reputation: 11

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);

Upvotes: 1

Saif
Saif

Reputation: 713

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);

Upvotes: 0

Related Questions