Android-async-http下载APK版本更新

创建http访问接口

public void downloadFile(String url, BinaryHttpResponseHandler responseHandler){
        Log.d(TAG, "checkToken url : " + url);
        httpClient.get(url, responseHandler);
    }

封装DownloadFile方法

private void downloadFile(String url){
        Log.d(TAG, "URL:" + url);
        String[] mAllowedContentTypes = new String[]{".*"};   //文件类型
        HttpManager.getInstance(mContext).downloadFile(url, new BinaryHttpResponseHandler(mAllowedContentTypes) {

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] bytes) {
                Log.d(TAG, "onSuccess:");
                Log.d(TAG, "共下载了:" + bytes.length);
                FileOutputStream fileOutputStream = null;
                String filePath = "xxx.apk";
                File file = new File(Environment.getExternalStorageDirectory(), filePath);
                if(isFileExist(filePath)){
                    deleteFile(filePath);
                }

                InputStream inputstream = new ByteArrayInputStream(bytes);
                if (inputstream != null) {
                    write2SDFromInput(file, inputstream);
                    try {
                        inputstream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFinish() {
                Log.d(TAG, "onFinish:");

                mCallback.onFinished();
                dismiss();
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "xxx.apk")),
                        "application/vnd.android.package-archive");
                mContext.startActivity(intent);
                super.onFinish();
            }

            @Override
            public void onCancel() {
                Log.d(TAG, "onCancel:");
                super.onCancel();
            }

            @Override
            public void onProgress(int bytesWritten, int totalSize) {
                loading_process = (int) ((bytesWritten * 1.0 / totalSize) * 100);
                Log.d(TAG, "onProgress:" + loading_process);
                pb.setProgress(loading_process);
                System.out.println("onProgressUpdate" + bytesWritten);
                tv.setText(String.format(mContext.getString(R.string.version_update_progress_content), loading_process));
                super.onProgress(bytesWritten, totalSize);
            }


            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

            }

        });
    }

    public void write2SDFromInput(File file, InputStream inputstream) {
        OutputStream output = null;
        try {
            output = new FileOutputStream(file);
// 4k为单位,每4K写一次
            byte buffer[] = new byte[4 * 1024];
            int temp = 0;
            while ((temp = inputstream.read(buffer)) != -1) {
// 获取指定信,防止写入没用的信息
                output.write(buffer, 0, temp);
            }
            output.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 在sdcard卡上创建文件
    public File createSDFile(String fileName) throws IOException {
        File file = new File(SDPATH + fileName);
        file.createNewFile();
        return file;
    }


    // 在sd卡上创建目录
    public File createSDDir(String dirName) {
        File dir = new File(SDPATH + dirName);
// mkdir只能创建一级目录 ,mkdirs可以创建多级目录
        dir.mkdir();
        return dir;
    }


    // 判断sd卡上的文件夹是否存在
    public boolean isFileExist(String fileName) {
        File file = new File(SDPATH + fileName);
        return file.exists();
    }


    public void deleteFile(String fileName) {
        File file = new File(SDPATH + fileName);
        file.delete();
    }


猜你喜欢

转载自blog.csdn.net/meifannao789456/article/details/47394631