2013-02-22 102 views
1

我點擊了我的apk文件所在的URL,然後將收到的字節寫入文件。APK文件在寫入SD卡時被損壞

class DownloadAPKFile extends AsyncTask<String, Void, Boolean>{ 

    private byte[] fileBytes; 

    @Override 
    protected Boolean doInBackground(String... params) { 
     Log.d("begin", "begun"); 
     HttpClient client = new DefaultHttpClient(); 
     HttpGet get = new HttpGet("http://www.website/Path/my.apk"); 


     try { 
      HttpResponse response = client.execute(get); 
      Log.d("Login", "Response " + response.getEntity()); 
      Log.d("Login", "contentLength " + response.getEntity().getContentLength()); 
      String responseBody = EntityUtils.toString(response.getEntity()); 
      fileBytes = responseBody.getBytes(); 

      Log.d("fileBytes", "fileBytes"); 
      String filePath = Environment.getExternalStorageDirectory() + "/myappdir/" + "my" + ".apk"; 

      File file = new File(filePath); 
      file.getParentFile().mkdirs(); 
      file.createNewFile(); 

      BufferedOutputStream objectOut = new BufferedOutputStream(new FileOutputStream(file)); 
      Log.d("objectOut", "objectOut"); 
      objectOut.write(fileBytes); 
      Log.d("write", "write"); 
      objectOut.close(); 



     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 


     return null; 
    } 

} 

這個工程就像一個魅力,我遇到的問題是,從entitiy內容長度爲582504,但是當我看着文件管理器的大小會高達863145。我認爲在將文件寫入SD卡時正在添加一些數據。有沒有解決這個問題的方法?

+0

我有完全相同的問題,我的APK具有873KB,當我將其導出。 我通過應用下載後,它有910,有時916kb ... 此外,它是一個無效的zip和Android無法讀取包信息。試圖安裝時給我一個分析錯誤。 – SparK 2014-05-26 14:32:33

回答

-1

這是我的代碼工作正常,請檢查是否該爲你的作品

public class downloadApk extends AsyncTask<Integer, Integer, Integer> 
    { 


     @Override 
     protected Integer doInBackground(Integer... params) { 
      // TODO Auto-generated method stub 

      try { 

       URL url = new URL("http://www.tagsinfosoft.com/android/shelf/Shelf_Cam.apk"); 
       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, "Shelf_Cam.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 

      } catch (Exception e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

      return null; 

     } 

     @Override 
     protected void onPostExecute(Integer result) { 
      // TODO Auto-generated method stub 
      super.onPostExecute(result); 
      Context context=shelf.this; 
      pd.dismiss(); 
      Intent intent = new Intent(Intent.ACTION_VIEW); 
      intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "Shelf_Cam.apk")), "application/vnd.android.package-archive"); 
      startActivity(intent); 

     } 

     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
      super.onPreExecute(); 
      pd=ProgressDialog.show(shelf.this,"Updating","Please wait....."); 
     } 

    } 
+0

這對於其他方式來說可能是好的,但我想要的是我的方法的解決方案。 – prometheuspk 2013-02-22 08:55:23

+1

這是5-6行不同於你的,如果是腐敗文件的情況下,它肯定會有所幫助,如果是內存問題,只要apk可用,這並不重要。 – 2013-02-22 09:01:47