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.