Dan
Dan

Reputation: 83

Parsing Error on downloading and running an .apk file

I have a feature in my app which allows the user to download a new version.
But my problem is when I start the intent which should start the installation proccess there is a message:

Parse error. There was a problem parsing the package

This is my code

Button button;
String urlApp = "example.com/app.apk"
String fileName = "app.apk";
DownloadManager downloadManager;

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

    button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            File file = new File(Environment.DIRECTORY_DOWNLOADS, fileName);
            if (file.exists()){
                boolean deleted = file.delete();
                Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_SHORT).show();
            }

            downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(urlApp));
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
            request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
            request.setTitle("Click to update");
            downloadManager.enqueue(request);
            registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));

        }
    });

}

private void openFile() {
    Intent install = new Intent(Intent.ACTION_VIEW);
    install.setDataAndType(Uri.fromFile(new File(Environment.DIRECTORY_DOWNLOADS+"/"+fileName)),
            "application/vnd.android.package-archive");
    startActivity(install);
}

BroadcastReceiver onComplete = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        openFile();
    }
};

}

It's just for .apk files, .txt files work fine. There is neither a error on the logcat. The deleting is also not working but this isn't my primary problem.

Thanks in advance :D

Sorry if this is a stupid problem but this is my first time working with the download manager

Upvotes: 3

Views: 1900

Answers (2)

Dan
Dan

Reputation: 83

Ok, i finally solved it

Instead of:

install.setDataAndType(Uri.fromFile(new File(Environment.DIRECTORY_DOWNLOADS+"/"+fileName)),
        "application/vnd.android.package-archive");

i used

    install.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/"+fileName)),
            "application/vnd.android.package-archive");

Upvotes: 1

Saumik Bhattacharya
Saumik Bhattacharya

Reputation: 941

I have come across the same problem earlier.

Please check the Min API level for your new Application version and the API level of the device you are using.

Upvotes: 0

Related Questions