@Override
  public byte[] downloadBitmap(String url, ImageConfig config) throws Exception {
    try {
      DownloadProcess progress = config.getProgress();

      if (progress != null) progress.sendPrepareDownload(url);

      ByteArrayOutputStream out = new ByteArrayOutputStream();

      // 加载assets目录
      InputStream in = GlobalContext.getInstance().getAssets().open(url);

      if (progress != null) progress.receiveLength(in.available());

      if (in == null) return null;

      // 获取图片数据
      byte[] buffer = new byte[1024 * 128];
      int readLen = -1;
      int readBytes = 0;
      while ((readLen = in.read(buffer)) != -1) {
        readBytes += readLen;
        if (progress != null) progress.sendProgress(readBytes);
        out.write(buffer, 0, readLen);
      }

      byte[] bs = out.toByteArray();
      in.close();
      out.close();

      return bs;
    } catch (Exception e) {
      if (config.getProgress() != null) config.getProgress().sendException(e);
      throw new Exception(e.getCause());
    }
  };
Пример #2
0
  @Override
  public byte[] downloadBitmap(String url, ImageConfig config) throws Exception {
    try {
      File imgFile = new File(url);
      if (imgFile.exists()) {
        DownloadProcess process = config.getProgress();

        if (process != null) process.prepareDownload(url);

        // 如果图片需要压缩,直接解析成bitmap
        if (config.getMaxHeight() > 0 || config.getMaxWidth() > 0) {
          Bitmap bitmap =
              BitmapDecoder.decodeSampledBitmapFromFile(
                  imgFile.getAbsolutePath(), config.getMaxWidth(), config.getMaxHeight());

          ByteArrayOutputStream out = new ByteArrayOutputStream();

          boolean isPng = url.toLowerCase().endsWith("png") ? true : false;
          bitmap.compress(isPng ? Bitmap.CompressFormat.PNG : Bitmap.CompressFormat.JPEG, 100, out);
          byte[] result = out.toByteArray();
          out.close();

          if (process != null) {
            process.sendLength(result.length);
            process.sendProgress(result.length);
            process.sendFinishedDownload(result);
          }

          Logger.w("直接解析sd卡图片,压缩尺寸");

          return result;
        } else {
          InputStream in = new FileInputStream(new File(url));

          if (process != null) process.sendLength(in.available());

          ByteArrayOutputStream out = new ByteArrayOutputStream();
          byte[] buffer = new byte[8 * 1024];
          int length = -1;
          long readBytes = 0;
          while ((length = in.read(buffer)) != -1) {
            readBytes += length;
            if (process != null) process.sendProgress(readBytes);
            out.write(buffer, 0, length);
          }
          out.flush();
          byte[] result = out.toByteArray();
          in.close();
          out.close();

          if (process != null) process.sendFinishedDownload(result);

          return result;
        }
      }

      if (config.getProgress() != null) config.getProgress().downloadFailed(null);
      throw new Exception("");
    } catch (Exception e) {
      if (config.getProgress() != null) config.getProgress().sendException(e);
      throw new Exception(e.getCause());
    }
  }