我目前正在開發一個應用程序,需要檢查是否有新版本可用。 我發現如何下載apk並安裝它。 但我無法弄清楚如何獲得APK URL。 我使用測試(alpha)環境,所以我的應用程序尚未發佈,我可以在哪裏獲得APK URL。 下面是下載的代碼:APK的文件位於android下載位置?
try {
URL url = new URL(apkurl);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = Environment.getExternalStorageDirectory() + "/download/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "app.apk");
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();//till here, it works fine - .apk is download to my sdcard in download file
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "app.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Update error!", Toast.LENGTH_LONG).show();
}
我怎樣才能獲得APK網址?
你下載的是創建文件夾apk to'Environment.getExternalStorageDirectory()+「/ download /」;'命名app.apk的目錄。並下載你打開它使用intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory()+「/ download /」+「app.apk」)),「application/vnd.android.package-archive 「);'那麼你得到的錯誤 –
我怎麼知道我應該下載文件到哪個文件夾? – Eli