gpt4 book ai didi

java - APK 文件写入 SD 卡时损坏

转载 作者:行者123 更新时间:2023-12-01 06:19:03 24 4
gpt4 key购买 nike

我访问了托管我的 apk 文件的 URL,然后将收到的字节写入文件。

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

}

这就像一个魅力,我遇到的问题是实体的内容长度是582504,但是当我查看文件管理器时,大小上升到863145 >。我认为在将文件写入 SD 卡时正在添加一些数据。有什么办法解决这个问题吗?

最佳答案

这是我的代码,运行良好,请检查这是否适合您

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

}

关于java - APK 文件写入 SD 卡时损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15018855/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com