Example #1
1
  public Bitmap getBitmap(String fileId, String size, boolean fromUrl) {
    File file = fileCache.getFile(fileId + size);

    // from SD cache
    Bitmap bitmap = decodeFile(file, size);

    if (bitmap != null) {
      return bitmap;
    }

    // from web
    try {
      bitmap = null;

      if (fromUrl) {
        InputStream is =
            ConnectionHandler.httpGetRequest(fileId, UsersManagement.getLoginUser().getId());
        OutputStream os = new FileOutputStream(file);
        Utils.copyStream(is, os);
        os.close();
        is.close();
      } else {
        CouchDB.downloadFile(fileId, file);
      }

      bitmap = decodeFile(file, size);

      return bitmap;
      // return ConnectionHandler.getBitmapObject(url);
    } catch (Throwable ex) {
      ex.printStackTrace();
      if (ex instanceof OutOfMemoryError) memoryCache.clear();
      return null;
    }
  }
Example #2
0
  public Bitmap getBitmap(String url) {
    File f = fileCache.getFile(url);

    // from SD cache
    Bitmap bitmap = decodeFile(f);

    if (bitmap != null) {
      Logger.error("ImageLoader", "fileCache.returnBitmap : " + url);
      return bitmap;
    }

    // from web
    try {

      bitmap = null;
      InputStream is =
          ConnectionHandler.httpGetRequest(url, UsersManagement.getLoginUser().getId());
      OutputStream os = new FileOutputStream(f);
      Utils.copyStream(is, os);
      os.close();
      is.close();
      // conn.disconnect();
      bitmap = decodeFile(f);

      return bitmap;
      // return ConnectionHandler.getBitmapObject(url);
    } catch (Throwable ex) {
      ex.printStackTrace();
      if (ex instanceof OutOfMemoryError) memoryCache.clear();
      return null;
    }
  }
Example #3
0
  // decodes image and scales it to reduce memory consumption
  private Bitmap decodeFile(File f, String size) {
    try {
      // decode image size
      BitmapFactory.Options o = new BitmapFactory.Options();
      o.inJustDecodeBounds = true;
      FileInputStream stream1 = new FileInputStream(f);
      BitmapFactory.decodeStream(stream1, null, o);
      stream1.close();

      int REQUIRED_SIZE = 200;
      // Find the correct scale value. It should be the power of 2.
      if (size.equals(SMALL)) {
        REQUIRED_SIZE = SIZE_SMALL;
      } else if (size.equals(LARGE)) {
        REQUIRED_SIZE = SIZE_LARGE;
      }
      int width_tmp = o.outWidth, height_tmp = o.outHeight;
      int scale = 1;
      while (true) {
        if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
      }
      // scale=4;
      // decode with inSampleSize
      BitmapFactory.Options o2 = new BitmapFactory.Options();
      o2.inSampleSize = scale;
      if (Utils.isOsVersionHigherThenGingerbread()) {
        o2.inPreferredConfig = Bitmap.Config.ARGB_8888;
      } else {
        o2.inPreferredConfig = Bitmap.Config.RGB_565;
      }
      FileInputStream stream2 = new FileInputStream(f);
      Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2);
      stream2.close();
      return bitmap;
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }