URL url = new URL(arg0[1]);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = "/mnt/sdcard/Android/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "myGame.apk");
if(outputFile.exists()){
outputFile.delete();
}
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("mnt/sdcard/Android/myGame.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
I want to do auto-update app function and it will download the apk file and install it. However when I download through Java and exception caught: java.io.FileNotFoundException http://192.168.2.143/myGame.apk
, where url[0]="http://192.168.2.143/myGame.apk", I use browser in my device to open http://192.168.2.143/myGame.apk
, the apk can be downloaded and inside sdcard/Download/ folder. I have INTERNET permission in my manifest. Any idea?
As Brijesh Thakur said
Remove c.setDoOutput(true)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With