예제 #1
0
  public static void setBitmapToFile(String cachePath, String imageName, Bitmap bitmap) {
    File file = null;
    FileOutputStream fos = null;
    try {
      file = new File(cachePath, imageName);
      if (!file.exists()) {
        File file2 = new File(cachePath);
        file2.mkdirs();
      }

      if (Utils.hasSDCard()) {
        fos = new FileOutputStream(file);
      } else {
        fos = context.openFileOutput(imageName, Context.MODE_PRIVATE);
      }
      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
      fos.flush();
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (fos != null) {
        try {
          fos.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
예제 #2
0
  /** 将下载好的图片存放到文件中 */
  private void setBitmapToFile(String imageName, InputStream is) {
    File file = null;
    FileOutputStream fos = null;
    try {
      file = new File(cacheDirPath_, imageName);
      if (!file.exists()) {
        File file2 = new File(cacheDirPath_);
        file2.mkdirs();
      }

      if (Utils.hasSDCard()) {
        fos = new FileOutputStream(file);
      } else {
        fos = context.openFileOutput(cacheDirName + "/" + imageName, Context.MODE_PRIVATE);
      }

      int len = -1;
      byte[] buffer = new byte[1024];
      while ((len = is.read(buffer)) != -1) {
        fos.write(buffer, 0, len);
        fos.flush();
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      if (fos != null) {
        try {
          fos.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      if (is != null) {
        try {
          is.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      Bitmap bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 100, 100);
      if (null != bitmap) {
        Bitmap compressBitmap = BitmapUtil.compressImage(bitmap);
        String cachePath = Utils.getCacheDir(context, "picture");
        setBitmapToFile(cachePath, imageName, compressBitmap);
      }
    }
  }