Hazem ENG Hazem ENG - 2 years ago 95
Android Question

How to sava file as apk without increasing in size ın Android(can not install it)

I try to download file from server and save it in internel storage and then install it.
I downloaded it using retrofit by this code

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");
}
});


and I saved it by this code

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;
}
}


its original 4.56MB but it shows 6.08MB in Android storage.
When I open it to install it shows
version N /A size N/ A
and when I try to install it, it shows this parse problem

Do you have any idea to solve this problem?
How to remove extra bytes?

Answer Source

You cloud make sure that must have used HttpResponseMessage.content in your webservice method because normal return will not be standart size for file that you want :

   [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;
  }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download