I’m building a private enterprise Android application. My goal is to detect when there is update available and offer user to download it. If user choose to download update file is downloaded and android app install prompt is showed.
I successfully check for update, the problem is that apk file is not downloaded (empty file is created) therefore “There is a problem parsing the package.” error is showed in android app install prompt.
Code:
public void downloadfileto(String fileurl, String filename) {
String myString;
try {
FileOutputStream f = new FileOutputStream(filename);
try {
URL url = new URL(fileurl);
URLConnection urlConn = url.openConnection();
InputStream is = urlConn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is, 8000);
int current = 0;
while ((current = bis.read()) != -1) {
f.write((byte) current);
}
} catch (Exception e) {
myString = e.getMessage();
}
f.flush();
f.close();
install(filename);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
protected void install(String fileName) {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(Uri.fromFile(new File(fileName)),
"application/vnd.android.package-archive");
startActivity(install);
}
Function downloadfileto is called with:
downloadfileto("http://some-url/ind.apk", "data/data/my.package.name/app.apk");
I would like to thank you all for helping here. I solved the problem by opening php script on server that counts downloads with web view, detecting download, path of download and starting activity to install application.
Name of file is always in form “ind-version.apk” (Example: ind-1-0.apk) and because I get version number of new update when I check for updates I decided to put it in extras and use it to determine file name.
Code:
And install: