写入SD卡时,APK文件已损坏

时间:2013-02-22 07:17:14

标签: java android file apk httpresponse

我点击了托管我的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卡时会添加一些数据。这有什么解决方案吗?

1 个答案:

答案 0 :(得分:-1)

这是我的代码,工作正常,请检查这是否适合你

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

    }