This is my code.
mUpdateNowView is a button and it's onClick method I download & update APK automatically.
I got an error while installing apk Parse Error There was a problem while parsing the package.
How can I resolve this error
mUpdateNowView.setOnClickListener(updateNow);
public View.OnClickListener updateNow = new View.OnClickListener() {
public void onClick(View v) {
try {
ProgressDialog progress;
InstallAPK downloadAndInstall = new InstallAPK();
progress = new ProgressDialog(SplashActivity.this);
progress.setCancelable(false);
progress.setMessage("Downloading...");
downloadAndInstall.setContext(getApplicationContext(), progress);
downloadAndInstall.execute("http://APK_URL.apk");
} catch (Exception e) {
}
}
};
This is my InstallAPK class, This class is used for downloading apk in Internal storage and install apk automatically.
public class InstallAPK extends AsyncTask<String, Void, Void> {
private ProgressDialog progressDialog;
private int status = 0;
private Context context;
public void setContext(Context context, ProgressDialog progress) {
this.context = context;
this.progressDialog = progress;
}
public void onPreExecute() {
progressDialog.show();
}
@Override
protected Void doInBackground(String... arg0) {
try {
URL url = new URL(arg0[0]);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
ContextWrapper cw = new ContextWrapper(context);
File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
File myDir = new File(directory, "Android/data/MyAPK");
myDir.mkdirs();
File outputFile = new File(myDir, "MyAPK.apk");
if (outputFile.exists()) {
outputFile.delete();
}
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is ;
int status = c.getResponseCode();
if (status != HttpURLConnection.HTTP_OK)
is = c.getErrorStream();
else
is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.flush();
fos.close();
is.close();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(directory, "Android/data/MyAPK/MyAPK.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} catch (FileNotFoundException fnfe) {
status = 1;
Log.e("File", "FileNotFoundException! " + fnfe);
} catch (Exception e) {
Log.e("UpdateAPP", "Exception " + e);
}
return null;
}
public void onPostExecute(Void unused) {
progressDialog.dismiss();
if (status == 1)
Toast.makeText(context, "MyAPK Not Available", Toast.LENGTH_LONG).show();
}
}
Please help me. Thanks in advance.