国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Android實現文件下載

瀏覽:4日期:2022-09-17 14:53:58
前言

總體思路:下載文件到應用緩存路徑,在相冊創建文件夾,Copy過去,通知相冊刷新。

下載文件到APP緩存路徑,這樣可避免Android高版本讀取本地權限問題,

準備

implementation ’com.squareup.okhttp3:okhttp:3.6.0’implementation ’com.squareup.okio:okio:1.11.0’調用

url:文件url

path:儲存的路徑

應用緩存路徑:getExternalCacheDir().getPath()

DownloadUtil.get().download(url, path, new DownloadUtil.OnDownloadListener() { @Override public void onDownloadSuccess(File file) {//下載成功 handler.sendEmptyMessage(1); } @Override public void onDownloading(int progress) {//進度條handler.sendEmptyMessage(progress); } @Override public void onDownloadFailed() {//下載失敗handler.sendEmptyMessage(-1); }});

復制到相冊目錄

DownloadUtil.createFiles(downLoadFile);

通知相冊刷新

DownloadUtil.updateDCIM(this,downloadFile);

工具類

/** * 下載工具類 * martin * 2021.7.21 */public class DownloadUtil { private static final String TAG = DownloadUtil.class.getName(); 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 oldPath$Name String 原文件路徑+文件名 如:data/user/0/com.test/files/abc.txt * @param newPath$Name String 復制后路徑+文件名 如:data/user/0/com.test/cache/abc.txt * @return <code>true</code> if and only if the file was copied; * <code>false</code> otherwise */ public static boolean copyFile(String oldPath$Name, String newPath$Name) {try { File oldFile = new File(oldPath$Name); if (!oldFile.exists()) {Log.e('--Method--', 'copyFile: oldFile not exist.');return false; } else if (!oldFile.isFile()) {Log.e('--Method--', 'copyFile: oldFile not file.');return false; } else if (!oldFile.canRead()) {Log.e('--Method--', 'copyFile: oldFile cannot read.');return false; } /* 如果不需要打log,可以使用下面的語句 if (!oldFile.exists() || !oldFile.isFile() || !oldFile.canRead()) {return false; } */ FileInputStream fileInputStream = new FileInputStream(oldPath$Name); FileOutputStream fileOutputStream = new FileOutputStream(newPath$Name); byte[] buffer = new byte[1024]; int byteRead; while (-1 != (byteRead = fileInputStream.read(buffer))) {fileOutputStream.write(buffer, 0, byteRead); } fileInputStream.close(); fileOutputStream.flush(); fileOutputStream.close(); return true;} catch (Exception e) { e.printStackTrace(); return false;} } /** * 通知相冊更新 * * @param context * @param newFile */ public void updateDCIM(Context context, File newFile) {File cameraPath = new File(dcimPath, 'Camera');File imgFile = new File(cameraPath, newFile.getAbsolutePath());if (imgFile.exists()) { Uri uri = Uri.fromFile(imgFile); Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); intent.setData(uri); context.sendBroadcast(intent);} } /** * url 下載連接 * saveDir 儲存下載文件的SDCard目錄 * listener 下載監聽 */ public void download(final String url, final String saveDir, 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(); } @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 savePath = isExistDir(saveDir);try { is = response.body().byteStream(); long total = response.body().contentLength(); File file = new File(savePath, getNameFromUrl(url)); 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();} finally { try {if (is != null) is.close(); } catch (IOException e) { } try {if (fos != null) fos.close(); } catch (IOException e) { }} }}); } /** * saveDir * 判斷下載目錄是否存在 */ private static String isExistDir(String saveDir) throws IOException {// 下載位置File downloadFile = new File(saveDir);if (!downloadFile.mkdirs()) { downloadFile.createNewFile();}String savePath = downloadFile.getAbsolutePath();return savePath; } //系統相冊路徑 static String dcimPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath(); /** * 創建文件夾 */ public static void createFiles(File oldFile) {try { File file = new File(isExistDir(dcimPath + '/xxx')); String newPath = file.getPath() + '/' + oldFile.getName(); Log.d('TAG', 'createFiles111: ' + oldFile.getPath() + '--' + newPath); if (copyFile(oldFile.getPath(), newPath)) {Log.d('TAG', 'createFiles222: ' + oldFile.getPath() + '--' + newPath); }} catch (IOException e) { e.printStackTrace();} } /** * url * 從下載連接中解析出文件名 */ @NonNull public static String getNameFromUrl(String url) {return url.substring(url.lastIndexOf('/') + 1); } public interface OnDownloadListener {/** * 下載成功 */void onDownloadSuccess(File file); /** * @param progress 下載進度 */void onDownloading(int progress); /** * 下載失敗 */void onDownloadFailed(); }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
主站蜘蛛池模板: 精品国产自在现线看久久 | 日本一区深夜影院深a | 在线播放第一页 | 亚洲美女视频一区二区三区 | 亚洲精品午夜一区二区在线观看 | 精品一区二区三区的国产在线观看 | 一本色道久久88综合亚洲精品高清 | 在线观看偷拍视频一区 | 亚洲视频一区二区三区 | 亚洲国产成人精品91久久久 | 黄 色 成 年人网站 黄 色 免费网 站 成 人 | www中文字幕在线观看 | 国产一级一片免费播放视频 | 精品日韩在线视频一区二区三区 | 一本伊大人香蕉高清在线观看 | 91精品国产美女福到在线不卡 | 欧美另类久久久精品 | 不卡精品国产_亚洲人成在线 | 免费一级毛片在线播放欧美 | 成人综合婷婷国产精品久久免费 | 久草福利资源网站免费 | 免费特黄一级欧美大片在线看 | 国产综合视频在线观看一区 | 久久在线 | 日韩午夜片 | 国产精品爽爽va在线观看无码 | 亚洲精品综合一区二区 | 自拍 欧美| 日本一级高清不卡视频在线 | 亚洲国产精品久久久久 | 欧美视频三级 | 男女做性免费视频软件 | a级男女性高爱潮高清试 | 国产成a人片在线观看视频 国产成版人视频网站免费下 | 香焦视频在线观看黄 | 亚洲一区二区三区久久久久 | 欧美成成人免费 | 国产精品久久久久久久久久久不卡 | 狼人 成人 综合 亚洲 | 一级免费视频 | 中文国产成人精品久久水 |