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.