I am downloading an Android app from a server and saving it to the internal space of my application using:
URL url = new URL(Urls[0]);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
File outputFile = new File(getFilesDir(), "Apps");
outputFile.mkdirs();
File file = makeFilename(outputFile, _appName + ".apk");
file.createNewFile();
file.setExecutable(true);
file.setReadable(true, false);
file.setWritable(true, false);
FileOutputStream fout = new FileOutputStream(zipFile);
InputStream in = conn.getInputStream();
byte[] buffer = new byte[1024];
int length = 0;
while ((length = in.read(buffer)) > 0) {
fout.write(buffer, 0, length);
}
fout.close();
in.close();
outputFile.setExecutable(true, false);
outputFile.setReadable(true, false);
outputFile.setWritable(true, false);
private File makeFilename(File base, String name) {
if (name.indexOf(File.separatorChar) < 0) {
return new File(base, name);
}
throw new IllegalArgumentException("File " + name
+ " contains a path separator");
}
when the download is completed, a notification appears. When I click this notification, the app should be installed. But in reality, when I click the notification, an “Error parsing the package” appears. What I am doing wrong?
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(new File(getFilesDir()+ "/Apps/_appName.apk")),"application/vnd.android.package-archive");
PendingIntent act = PendingIntent.getActivity(MainActivity._activity,0, i, 0);
notification.setLatestEventInfo("TEXT", "TEXT, act);
I have a doubt for your downloaded .apk file is Private to your application itself.
So just check it,
And also when you store file in your application make sure it has, WORLD_READABLE Permission like,
Context.MODE_WORLD_READABLESomething like,