下载
下载路径
- 为了避免权限申请的麻烦,我们可以将文件下载到 Android/data/{应用包名}下,操作这个路径下的文件是不需要存储读写权限的
File cacheDir = context.getExternalCacheDir();
File filesDir = context.getExternalFilesDir(null);
下载文件过程
public static void downloadFile(File file, String fileDownloadUrl, ProgressBar progressBar) throws Exception {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
URL url = new URL(fileDownloadUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Accept-Encoding", "identity");
conn.setConnectTimeout(5000);
conn.connect();
progressBar.setMax(conn.getContentLength()