2017-06-15 35 views
2

我嘗試從服務器下載文件並將其保存在internel存儲中,然後進行安裝。 我下載使用改造這段代碼如何在不增大尺寸的情況下將文件另存爲Android Android(無法安裝)

Call<ResponseBody> call = apiService.getNewVersion(); 

    call.enqueue(new Callback<ResponseBody>() { 
     @Override 
     public void onResponse(Call<ResponseBody> call, 
Response<ResponseBody> response) { 
      if (response.isSuccessful()) { 
       Log.d("1", "server contacted and has file"); 

       boolean writtenToDisk = 
writeResponseBodyToDisk(response.body()); 

       Log.d("2", "file download was a success? " + writtenToDisk); 
      } else { 
       Log.d("3", "server contact failed"); 
      } 
     } 

     @Override 
     public void onFailure(Call<ResponseBody> call, Throwable t) { 
      Log.e("4", "error"); 
     } 
    }); 

,我這段代碼

private boolean writeResponseBodyToDisk(ResponseBody body) { 
    try { 
     // todo change the file location/name according to your needs 
     File futureStudioIconFile = new 
File(context.getExternalFilesDir(null) + File.separator + "app-debug.apk"); 

     InputStream inputStream = null; 
     OutputStream outputStream = null; 

     try { 
      byte[] fileReader = new byte[4096]; 

      long fileSize = body.contentLength(); 
      long fileSizeDownloaded = 0; 

      inputStream = body.byteStream(); 
      outputStream = new FileOutputStream(futureStudioIconFile); 

      while (true) { 
       int read = inputStream.read(fileReader); 

       if (read == -1) { 
        break; 
       } 

       outputStream.write(fileReader, 0, read); 

       fileSizeDownloaded += read; 

       Log.d("TAG", "file download: " + fileSizeDownloaded + " of " 
    + fileSize); 
      } 

      outputStream.flush(); 

      return true; 
     } catch (IOException e) { 
      return false; 
     } finally { 
      if (inputStream != null) { 
       inputStream.close(); 
      } 

      if (outputStream != null) { 
       outputStream.close(); 
      } 
     } 
    } catch (IOException e) { 
     return false; 
    } 
} 

原來4.56MB保存它,但它顯示了在Android的存儲6.08MB。 當我打開它來安裝它顯示 version N /A size N/ A ,當我嘗試安裝它,它顯示了這個parse problem

你有任何想法來解決這個問題? 如何刪除多餘的字節?

回答

1

你應該確保您使用HttpResponseMessage.content在Web服務的方法,因爲一個正常的回報不會是你想要的文件的標準尺寸:

[HttpGet] 
[Route("api/Depository/DownloadFileFTP")] // /{pass} 
public HttpResponseMessage DownloadFileFTP() //string pass 
{ 
    HttpResponseMessage result = null; 
    string ftphost = "*******"; 
    string ftpfilepath = "/app-debug.apk"; 
    byte[] fileData; 

    string ftpfullpath = "ftp://" + ftphost + ftpfilepath; 

    using (WebClient request = new WebClient()) 
    { 
     request.Credentials = new NetworkCredential("**", "****"); 
     fileData = request.DownloadData(ftpfullpath); 
    } 
    byte[] bytes = fileData;    
    result = Request.CreateResponse(HttpStatusCode.OK); 
    result.Content = new ByteArrayContent(bytes); 
    result.Content.Headers.ContentDisposition = new 
    System.Net.Http.Headers.ContentDispositionHeaderValue("attachment"); 
    result.Content.Headers.ContentDisposition.FileName = "app.apk"; 
    return result; 
} 
+0

這是由於工作 – Hazem

相關問題