android 下载工具类,贼好用
使用方法:只需要传入一个下载的文件路径+文件名,然后再传入一个要下载的文件的url
该下载工具类需要和 okhttp网络请求库一起使用,如果你项目内没有,需要引入此库才能使用 implementation("com.squareup.okhttp3:okhttp:3.14.1")
DownloadUtil.get().download(url, getDiskCacheDir(mContext).toString(), "file."+hzname, new DownloadUtil.OnDownloadListener() { @Override public void onDownloadSuccess(File file) {//下载成功 Log.e("file path",""+file.toString()); readerView.post(new Runnable() { @Override public void run() { //存储一份 待网络不可用时打开此链接 Storage.saveFilePath(file.toString()); openFile(file.toString()); } }); } @Override public void onDownloading(int progress) {//下载进度 Log.e("progress:===",progress+""); } @Override public void onDownloadFailed(Exception e) {//下载异常 if (!Storage.getFilePath().equals("")) { openFile(Storage.getFilePath()); } } });
获取app包名目录下的下载路径
public String getDiskCacheDir(Context context) { String cachePath = null; if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable()) { cachePath = context.getExternalCacheDir().getPath(); } else { cachePath = context.getCacheDir().getPath(); } return cachePath; }
//具体的下载工具类
/** * Created by alert on 2020/08/21. */ public class DownloadUtil { private static DownloadUtil downloadUtil; private final OkHttpClient okHttpClient; public static DownloadUtil get() { if (downloadUtil == null) { downloadUtil = new DownloadUtil(); } return downloadUtil; } private DownloadUtil() { okHttpClient = new OkHttpClient(); } /** * @param url 下载连接 * @param destFileDir 下载的文件储存目录 * @param destFileName 下载文件名称 * @param listener 下载监听 */ public void download(final String url, final String destFileDir, final String destFileName, final OnDownloadListener listener) { Request request = new Request.Builder().url(url).build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { // 下载失败监听回调 listener.onDownloadFailed(e); } @Override public void onResponse(Call call, Response response) throws IOException { InputStream is = null; byte[] buf = new byte[2048]; int len = 0; FileOutputStream fos = null; // 储存下载文件的目录 File dir = new File(destFileDir); if (!dir.exists()) { dir.mkdirs(); } File file = new File(dir, destFileName); try { is = response.body().byteStream(); long total = response.body().contentLength(); 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); // 下载中更新进度条 listener.onDownloading(progress); } fos.flush(); // 下载完成 listener.onDownloadSuccess(file); } catch (Exception e) { listener.onDownloadFailed(e); } finally { try { if (is != null) is.close(); } catch (IOException e) { } try { if (fos != null) fos.close(); } catch (IOException e) { } } } }); } public interface OnDownloadListener { /** * @param file 下载成功后的文件 */ void onDownloadSuccess(File file); /** * @param progress 下载进度 */ void onDownloading(int progress); /** * @param e 下载异常信息 */ void onDownloadFailed(Exception e); }