0

I got this error while downloading file from url (Parse Error : There is a problem parsing the package.)

I googled, they say the problem while installing the android application this message appear, but my problem is different the android app is run fine but when I add the code to download file from url and click download button I got this error message

The strange thing that this message show on some device not all !!

This is my code to download apk file from url :

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

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

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           new DownloadFileFromURL().execute(LATEST_VERSION_URL);
        }
    });
}

And DownloadFileFromURL class :

 /**
 * Background Async Task to download file
 * */
class DownloadFileFromURL extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread
     * Show Progress Bar Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progress = ProgressDialog.show(MainActivity.this, "", "Updating, please wait this may take a few minutes...", true);
    }

    /**
     * Downloading file in background thread
     * */
    @Override
    protected String doInBackground(String... f_url) {
        int count;
        try {
            URL url = new URL(f_url[0]);
            URLConnection conection = url.openConnection();
            conection.connect();
            // getting file length
            int lenghtOfFile = conection.getContentLength();

            // input stream to read file - with 8k buffer
            InputStream input = new BufferedInputStream(url.openStream(), 8192);

            // Output stream to write file
            OutputStream output = new FileOutputStream("/sdcard/myapp.apk");

            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
                total += count;
                output.write(data, 0, count);
            }

            // flushing output
            output.flush();

            // closing streams
            output.close();
            input.close();

        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }

        return null;
    }

    /**
     * Updating progress bar
     * */
    protected void onProgressUpdate(String... progress) {
    }

    /**
     * After completing background task
     * Dismiss the progress dialog
     * **/
    @Override
    protected void onPostExecute(String file_url) {
        progress.dismiss();
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "myapp.apk")), "application/vnd.android.package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
        startActivity(intent);
    }

Anyone help me please I'm in stuck with this problem for more than 2 days

Thanks.

Update I solve it :)

First the problem was in Android 6(Marshmallow), even though the user accepted all your permissions at install time, they can later decide to take some of those permissions away from you so ti throws

java.io.IOException: open failed: EACCES (Permission denied)

so in my code above the exception throws and catch it then go to onPostExecute and try to open the apk file and install it but actually the file didn't download cuz this exception.

  • 1
    You need to provide more information. Are you certain that is how the error message is spelt? Does the App crash? Are there associated stack traces to the error? – Knossos Dec 16 '16 at 10:17
  • Sorry about this stupid error, I changed the language in my phone to US language & edit my post to true spelt – بسمة أمل Dec 16 '16 at 10:32
  • Possible duplicate of ["Parse Error : There is a problem parsing the package" while installing Android application](http://stackoverflow.com/questions/1492401/parse-error-there-is-a-problem-parsing-the-package-while-installing-android) – anna_manzhula Dec 16 '16 at 10:32
  • @anna_manzhula I checked the link and try the suggestion solution and it is not help me I don't export apk and run it I test immediately using usb, The first time my app is run but when I added the code above this problem occurred – بسمة أمل Dec 16 '16 at 10:45
  • I edit my post and add more explain please help me, thanks a lot – بسمة أمل Dec 16 '16 at 11:36
  • you didn't change min api version in your gradle? this error may cause in case min api > device android vesion – anna_manzhula Dec 16 '16 at 11:49
  • No I didn't change min app version , the app is install and run but when I click download button this error appear for some device not all – بسمة أمل Dec 16 '16 at 12:12

0 Answers0