2013-02-22 105 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