2016-05-17 219 views
0

我目前正在開發一個應用程序,需要檢查是否有新版本可用。 我發現如何下載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網址?

+0

你下載的是創建文件夾apk to'Environment.getExternalStorageDirectory()+「/ download /」;'命名app.apk的目錄。並下載你打開它使用intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory()+「/ download /」+「app.apk」)),「application/vnd.android.package-archive 「);'那麼你得到的錯誤 –

+0

我怎麼知道我應該下載文件到哪個文件夾? – Eli

回答

0

如果您使用的是Alpha(測試)環境。這是網址:

https://play.google.com/apps/testing/com.yourdomain.package 

但是,您的APK將不對外公開,​​它僅適用於alpha部分中激活的測試人員。

+0

好的,感謝它,我怎麼知道要下載文件的文件夾? – Eli

2

你必須自己創建它。創建文件夾併爲其命名文件。正如你在代碼中所做的那樣。像

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