[英]Where is located the APK file for download in android?
我目前正在開發需要檢查是否有可用新版本的應用程序。 我找到了如何下載並安裝APK。 但我不知道如何獲取APK URL。 我正在使用測試(alpha)環境,因此我的應用尚未發布,我可以在哪里獲得APK URL。 這是下載代碼:
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 URL?
您必須自己創建它。 創建文件夾並為其命名文件。 就像您在代碼中所做的一樣。 喜歡
String PATH = Environment.getExternalStorageDirectory() + "/download/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "app.apk");
創建自己的一個做到這一點。
在清單中添加此權限,
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
並使用文件名創建文件夾
File folder = new File(context.getCacheDir().getPath() + "/files/myFolder");
folder.mkdirs();
File f= new File(folder, "app.apk");
if (!folder .exists()){
folder.create();
}
您可以在其中放置APK
如果您正在使用Alpha(測試)環境。 這是網址:
https://play.google.com/apps/testing/com.yourdomain.package
但是,您的APK將無法公開使用,只有Alpha部分中的激活測試人員可以使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.