onCreat()方法中调用,申请权限//成员变量 private ProgressDialog progressDialog; private static final int REQUEST_EXTERNAL_STORAGE = 1; private static String[] PERMISSIONS_STORAGE = {"android.permission.READ_EXTERNAL_STORAGE","android.permission.WRITE_EXTERNAL_STORAGE"};
// 动态请求sd卡权限 verifyStoragePermissions(this);
private void okDownFile(final String serveletUrl, final String fileName) { progressDialog = new ProgressDialog(act); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setCancelable(false); progressDialog.show(); //新建一个File,传入文件夹目录 String SDPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/bdbook/"; File file1 = new File(SDPath); //判断文件夹是否存在,如果不存在就创建,否则不创建 if (!file1.exists()) { //通过file的mkdirs()方法创建目录中包含却不存在的文件夹 file1.mkdirs(); } OkHttpClient okHttpClient = new OkHttpClient(); try { Request request = new Request.Builder().url(serveletUrl).build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { Log.i("SS", "下载失败"); progressDialog.dismiss(); } @Override public void onResponse(Call call, Response response) throws IOException { InputStream is = null; byte[] buf = new byte[2048]; int len = 0; FileOutputStream fos = null; String SDPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/bdbook/"; try { is = response.body().byteStream(); long total = response.body().contentLength(); // File file = new File(SDPath, serveletUrl.substring(serveletUrl.lastIndexOf("/") + 1)); File file = new File(SDPath,fileName); fos = new FileOutputStream(file); long sum = 0; while ((len = is.read(buf)) != -1) { fos.write(buf, 0, len); sum += len; int progress = (int) (sum * 1.0f / total * 100); Log.i("SS", "progress=" + progress); Message msg = handler.obtainMessage(); msg.what = 1; msg.arg1 = progress; handler.sendMessage(msg); } fos.flush(); Log.i("SS", "文件下载成功"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (is != null) is.close(); } catch (IOException e) { e.printStackTrace(); } try { if (fos != null) fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }); } catch (Exception e) { ToastUtils.show(TypeActivity.this, "文件下载失败", Toast.LENGTH_SHORT); progressDialog.dismiss(); } } private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what) { case 1: //进度条的值 int i = msg.arg1; progressDialog.setProgress(i); } if (msg.arg1 == 100) { ToastUtils.show(TypeActivity.this, "文件下载成功", Toast.LENGTH_SHORT); progressDialog.dismiss(); } } };
//申请权限
public static void verifyStoragePermissions(Activity activity) { try { //检测是否有写的权限 int permission = ActivityCompat.checkSelfPermission(activity, "android.permission.WRITE_EXTERNAL_STORAGE"); if (permission != PackageManager.PERMISSION_GRANTED) { // 没有写的权限,去申请写的权限,会弹出对话框 ActivityCompat.requestPermissions(activity, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE); } } catch (Exception e) { e.printStackTrace(); } }