zoukankan      html  css  js  c++  java
  • 下载图片封装一个工具类,搞定图片批量下载

    改章节笔者在北京喝咖啡的时候突然想到的...这几周就有想写几篇关于下载图片的笔记,所以回家到之后就奋笔疾书的写出来发布了

       项目中用到的,要求一次下载30张图片。开始时使用谷歌bitmap fun中供给的ImageFetcher来下载,但是发现一个蛋疼非常现象,图片总是莫名其妙的少几张。

        

       排除了图片地址存在无效链接外,怀疑是并发下载线程个数太多,线程池满了以后,使用抛弃策略将之前的下载线程抛弃了。

        

       求人不如求己,自己写一个吧。

        

       在这里使用线程池,支撑并发下载。线程池可以自己选择,使用newSingleThreadExecutor,newFixedThreadPool,newCachedThreadPool中的任意一种。使用时,自己实现监听器,当监听下载个数与url集合的个数相同时,会回调监听器的onSuccess()方法。

        


        

       源码如下,希望大家教正
        每日一道理
    生命,是一场漫长的棋局。这盘棋没有猎猎西风,没有四起狼烟,只有在取舍和进退中抉择。只有像棋中的小卒那样,勇往直前,毫不退缩沿着沟沟坎坎的人生之路,艰难而执着的求索,前进,才会谱写人生最壮丽的强者之歌。

        import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.RejectedExecutionException; import android.util.Log; /** * 批量图片下载类 无需与界面交互的下载类 * * @Description: * * @author- liubing * @create- 2013-5 -6 * @modify by: * @modify- time */ public class DownloadService { private static String TAG = "DownloadService" ; public static final int IO_BUFFER_SIZE = 8 * 1024; private static final String CACHE_FILENAME_PREFIX = "cache_"; private static ExecutorService SINGLE_TASK_EXECUTOR = null; private static ExecutorService LIMITED_TASK_EXECUTOR = null; private static final ExecutorService FULL_TASK_EXECUTOR = null; private static final ExecutorService DEFAULT_TASK_EXECUTOR ; private static Object lock = new Object(); static { // SINGLE_TASK_EXECUTOR = (ExecutorService) // Executors.newSingleThreadExecutor(); LIMITED_TASK_EXECUTOR = (ExecutorService) Executors . newFixedThreadPool(1); // FULL_TASK_EXECUTOR = (ExecutorService) // Executors.newCachedThreadPool(); DEFAULT_TASK_EXECUTOR = LIMITED_TASK_EXECUTOR ; }; // 下载状态监听,供给回调 DownloadStateListener listener; // 下载目录 private String downloadPath; // 下载链接集合 private List<String> listURL; // 下载个数 private int size = 0; // 下载实现回调接口 public interface DownloadStateListener { public void onFinish(); public void onFailed(); } public DownloadService(String downloadPath, List<String> listURL, DownloadStateListener listener) { this.downloadPath = downloadPath; this.listURL = listURL; this.listener = listener; } /** * 暂未供给设置 */ public void setDefaultExecutor() { } /** * 开始下载 */ public void startDownload() { // 首先检测path是不是存在 File downloadDirectory = new File(downloadPath ); if (!downloadDirectory.exists()) { downloadDirectory.mkdirs(); } for (final String url : listURL) { //捕获线程池拒绝执行异常 try { // 线程放入线程池 DEFAULT_TASK_EXECUTOR.execute(new Runnable() { @Override public void run() { downloadBitmap(url); } }); } catch (RejectedExecutionException e) { e.printStackTrace(); Log. e(TAG, "thread pool rejected error"); listener.onFailed(); } catch (Exception e) { e.printStackTrace(); listener.onFailed(); } } } /** * 下载图片 * * @param urlString * @return */ private File downloadBitmap(String urlString) { String fileName = urlString; // 图片定名方式 final File cacheFile = new File(createFilePath(new File( downloadPath), fileName)); HttpURLConnection urlConnection = null; BufferedOutputStream out = null; try { final URL url = new URL(urlString); urlConnection = (HttpURLConnection) url.openConnection(); final InputStream in = new BufferedInputStream( urlConnection.getInputStream(), IO_BUFFER_SIZE); out = new BufferedOutputStream(new FileOutputStream(cacheFile), IO_BUFFER_SIZE); int b; while ((b = in.read()) != -1) { out.write(b); } // 每下载胜利一个,统计一下图片个数 statDownloadNum(); return cacheFile; } catch (final IOException e) { // 有一个下载失败,则表现批量下载没有胜利 Log. e(TAG, "download " + urlString + " error"); listener.onFailed(); } finally { if (urlConnection != null) { urlConnection.disconnect(); } if (out != null ) { try { out.close(); } catch (final IOException e) { Log. e(TAG, "Error in downloadBitmap - " + e); } } } return null ; } /** * Creates a constant cache file path given a target cache directory and an * image key. * * @param cacheDir * @param key * @return */ public static String createFilePath(File cacheDir, String key) { try { // Use URLEncoder to ensure we have a valid filename, a tad hacky // but it will do for // this example return cacheDir.getAbsolutePath() + File.separator + CACHE_FILENAME_PREFIX + URLEncoder.encode(key.replace("*", ""), "UTF-8" ); } catch (final UnsupportedEncodingException e) { Log. e(TAG, "createFilePath - " + e); } return null ; } /** * 统计下载个数 */ private void statDownloadNum() { synchronized (lock ) { size++; if (size == listURL .size()) { Log. d(TAG, "download finished total " + size); // 释放资源 DEFAULT_TASK_EXECUTOR.shutdownNow(); // 如果下载胜利的个数与列表中 url个数一致,说明下载胜利 listener.onFinish(); // 下载胜利回调 } } } }

      使用方法如下:

        new DownloadService( "/mnt/sdcard/test", listUrl, new DownloadStateListener() { @Override public void onFinish() { //图片下载胜利后,实现您的代码 } @Override public void onFailed() { //图片下载胜利后,实现您的代码 } }).startDownload();

      原文链接:

        http://www.67tgb.com/?p=590

    文章结束给大家分享下程序员的一些笑话语录: 看新闻说中国输入法全球第一!领先了又如何?西方文字根本不需要输入法。一点可比性都没有。

    --------------------------------- 原创文章 By
    下载和图片
    ---------------------------------

  • 相关阅读:
    关于此主题 v1
    从博客园主题了解前端 CSS
    VS2019 许可证到期
    从博客园主题了解前端 HTML
    关于此主题
    从博客园主题了解前端 JS
    GCC 编译器
    Python的Set和List的性能比较 + 两者之间的转换
    wsgi初探(转)
    权限设计概要
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3102390.html
Copyright © 2011-2022 走看看