简体   繁体   English

写入SD卡时APK文件损坏

[英]APK file gets corrupted while writing to SD Card

I hit to a URL where my apk file is hosted and then write the bytes received to a file. 我打到一个托管我的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;
    }

}

This works like a charm, the problem i am having is that the content length from the entitiy is 582504 but when i look into the file manager the size goes upto 863145 . 这就像一个魅力,我遇到的问题是582504的内容长度为582504但是当我查看文件管理器时,大小863145 I think that some data is being added while writing file to SD Card. 我认为将文件写入SD卡时正在添加一些数据。 Is there any solution to this? 有什么解决办法吗?

This is my code which works fine, please check if this works for you 这是我的代码,可以正常工作,请检查是否适合您

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

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM