我點擊了我的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卡時正在添加一些數據。有沒有解決這個問題的方法?
我有完全相同的問題,我的APK具有873KB,當我將其導出。 我通過應用下載後,它有910,有時916kb ... 此外,它是一個無效的zip和Android無法讀取包信息。試圖安裝時給我一個分析錯誤。 – SparK 2014-05-26 14:32:33