Ejemplo n.º 1
0
  /**
   * TODO 保存图片至sd卡
   *
   * @param fileName 文件名
   * @param bitmap
   */
  public static synchronized void saveToSD(final String fileName, final Bitmap bitmap) {
    if (bitmap == null || fileName == null) {
      return;
    }
    Runnable runnable =
        new Runnable() {

          public void run() {
            FileOutputStream b = null;
            try {
              b = new FileOutputStream(fileName);

              bitmap.compress(Bitmap.CompressFormat.PNG, 100, b); // 把数据写入文件
            } catch (Exception e) {
              e.printStackTrace();
            } finally {
              try {
                b.flush();
                b.close();
              } catch (Exception e) {
                e.printStackTrace();
              }
            }
          };
        };
    ThreadPoolManager.getInstance().addTask(runnable);
  }
Ejemplo n.º 2
0
  /**
   * 方法描述:新:从本地或者服务端加载图片
   *
   * @param context 上下文
   * @param imageLocalPath 本地路径
   * @param imagePath 网络图片url
   * @param callback 图片回调
   * @param isCompress 是否要压缩
   * @return Bitmap对象
   */
  private static Bitmap loadImage(
      final Context context,
      final String imageLocalPath,
      final String imagePath,
      final ImageCallback callback,
      final boolean isCompress) {
    Bitmap bitmap = null;
    if (imageLocalPath != null) // 从内存中高速加载
    bitmap = getBitmapFromCache(imageLocalPath);
    if (bitmap != null) {
      return bitmap;
    } else { // 从网上或本地缓存中加载
      final Handler handler =
          new Handler() {
            @Override
            public void handleMessage(Message msg) {
              if (msg.obj != null) {
                Bitmap bitmap = (Bitmap) msg.obj;
                if (callback != null) {
                  callback.loadImage(bitmap, imageLocalPath);
                }
              }
            }
          };
      Runnable runnable =
          new Runnable() {

            @Override
            public void run() {
              try {
                Message msg = handler.obtainMessage();
                // 检查本地缓存是否存在
                File imageFile = new File(imageLocalPath);
                if (imageFile.exists()) {
                  LogUtils.d("图片本地缓存加载url=:" + imageLocalPath);
                  // 从本地缓存中获取图片并存入常用集合中
                  putImageToMap(imageLocalPath, isCompress);
                  msg.obj = getBitmapFromCache(imageLocalPath);
                  if (msg.obj != null) {
                    handler.sendMessage(msg);
                  }
                } else { // 不存在则从网络获取
                  if (imagePath != null) {
                    Bitmap bitmap = null;
                    SharedPreferences sp =
                        context.getSharedPreferences("config", Context.MODE_PRIVATE);
                    // 仅在wifi下加载
                    if (sp.getBoolean("isWifiLoad", false)) {
                      if (!NetUtil.isWifi(context)) {
                        return;
                      }
                    }
                    URL url = new URL(imagePath);
                    LogUtils.d("图片从服务器加载url=:" + imagePath);
                    URLConnection conn = null;
                    conn = url.openConnection();
                    conn.setConnectTimeout(5000);
                    conn.setReadTimeout(5000);
                    conn.connect();
                    BufferedInputStream bis = new BufferedInputStream(conn.getInputStream(), 8192);
                    if (bis != null) {
                      bitmap = BitmapFactory.decodeStream(bis);
                    }
                    msg.what = 0;
                    putBitmapToMap(imageLocalPath, bitmap);
                    msg.obj = getBitmapFromCache(imageLocalPath);
                    if (msg.obj != null) {
                      handler.sendMessage(msg);
                    }
                    try {
                      // 保存文件到sd卡
                      saveToSD(imageLocalPath, bitmap);
                    } catch (Exception e) {
                      e.printStackTrace();
                      Log.e(ImageUtil.class.getName(), "保存图片至SD卡出错!");
                    }
                  }
                }
              } catch (Exception e) {
                Log.e(ImageUtil.class.getName(), "网络请求图片出错!");
                e.printStackTrace();
              }
            }
          };
      // 执行线程
      ThreadPoolManager.getInstance().addTask(runnable);
    }
    return null;
  }