0

I'm creating application custom updater, got Parse error while updating.

I download signed apk using AsyncTask

output = new FileOutputStream(context.getFilesDir()+"/update.apk");

Download completes succesfully, then I try to run apk like this:

File file = new File("file://"+context.getFilesDir(), "update.apk");
file.setReadable(true, false);
Intent promptInstall = new Intent(Intent.ACTION_VIEW)
    .setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");

promptInstall.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
promptInstall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(promptInstall);

App starts to update, but then gomes my issue,

Parse Error: An error occurred while parsing the package

What am I doing wrong?

EspeH
  • 1,308
  • 2
  • 16
  • 34

1 Answers1

0

FOUND SOLUTION..

I forgot to add permission to read from external storage, added this line into

AndroidManifest.xml

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

AsyncTask

@Override
protected String doInBackground (String...sUrl){
    ......
    File sd = Environment.getExternalStorageDirectory();
    File outputFile = new File(sd, "/download/update.apk");
    output = new FileOutputStream(outputFile);
    ......
}


@Override
protected void onPostExecute(String result) {
    ......
    File sd = Environment.getExternalStorageDirectory();
    File inputFile = new File(sd, "/download/update.apk");

    Intent promptInstall = new Intent(Intent.ACTION_VIEW);
    promptInstall.setDataAndType(Uri.fromFile(inputFile), "application/vnd.android.package-archive");

    promptInstall.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    promptInstall.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(promptInstall);
    ......
}
EspeH
  • 1,308
  • 2
  • 16
  • 34