0

I make an Android App, and i make a feature about update.

I download an .apk file and use intent to install it.But it always has an error like "there was a problem when parsing the package"

my code is

I use a receiver to listen the action when download complete ,code is

private BroadcastReceiver mBroadcaseReceiver;
protected void onCreate(@Nullable Bundle savedInstanceState) {
mCheckUpdateBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("AboutUsActivity","check update");
            downloadApk();
        }
    });
mBroadcaseReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)){
                Log.d("aboutusactivity","下载完成");
                //下载完毕后安装
                installApk();
            }
        }
    };
    registerReceiver(mBroadcaseReceiver,new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}


private void downloadApk() {
    Log.d("AboutusActivity","update");
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse("XXXXXX"));
    request.setDescription("updating");
    request.setTitle("title");
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
    }
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "yuedong.apk");

    // 获得下载服务和队列文件
    DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    manager.enqueue(request);
}

private void installApk() {
    Intent mIntent = new Intent(Intent.ACTION_VIEW);
    mIntent.setDataAndType(Uri.fromFile(new File(Environment.DIRECTORY_DOWNLOADS,"yuedong.apk")),
            "application/vnd.android.package-archive");
    this.startActivity(mIntent);
}

But it always like error So what's wrong with my code?

schnatterer
  • 7,525
  • 7
  • 61
  • 80
PPTing
  • 150
  • 1
  • 1
  • 12

3 Answers3

0

The app file .apk, that you have downloaded might be corrupted. If you try to install the corrupted apps, you will get the parse error "There was a problem parsing the package". So, try again downloading the app completely and install it.

Shishupal Shakya
  • 1,632
  • 2
  • 18
  • 41
0

Its may be because that file having private mode protection(access permission).Try this link.

Community
  • 1
  • 1
Chandra Sharma
  • 1,339
  • 1
  • 12
  • 26
0

I know the reason now because my path which download the apk is not match the path that i choose to install the apk. so stupid i am. i change it like

private void downloadApk(String url) {
    Log.d(TAG,"download");
    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
    request.setDescription("updating");
    request.setTitle("My app");

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
    }
    request.setDestinationInExternalPublicDir("/xxx/","update.apk");
    // 获得下载服务和队列文件
    DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    manager.enqueue(request);
}

private void installApk() {
    File mFile;
    mFile = new File(Environment.getExternalStorageDirectory()+"/xxx/update.apk");
    if (mFile.exists()){
        Intent mIntent = new Intent(Intent.ACTION_VIEW);
        mIntent.setDataAndType(Uri.parse("file://"+mFile.getAbsolutePath()),
                "application/vnd.android.package-archive");
        startActivity(mIntent);
    }else {
        Log.d(TAG,"the file is not exist");
    }

}
PPTing
  • 150
  • 1
  • 1
  • 12