Esempio n. 1
0
 @SuppressWarnings("unchecked")
 @Override
 protected Bitmap processBitmap(Object data) {
   FlowObjectToStringWrapper<Photo> fo = (FlowObjectToStringWrapper<Photo>) data;
   Photo imageData = fo.getObject();
   double ratio =
       imageData.getHeight() == 0
           ? 1
           : (float) imageData.getWidth() / (float) imageData.getHeight();
   int height = mImageHeight;
   int width = (int) (height * ratio);
   Bitmap result = null;
   try {
     imageData = PhotoUtils.validateUrlForSizeExistAndReturn(imageData, thumbSize);
     result = super.processBitmap(fo.toString(), width, height);
   } catch (Exception e) {
     GuiUtils.noAlertError(TAG, e);
   }
   return result;
 }
Esempio n. 2
-1
  /**
   * Download a bitmap from a URL, write it to a disk and return the File pointer. This
   * implementation uses a simple disk cache.
   *
   * @param context The context to use
   * @param urlString The URL to fetch
   * @return A File pointing to the fetched bitmap
   */
  public static File downloadBitmap(Context context, String urlString) {
    final File cacheDir = DiskLruCache.getDiskCacheDir(context, HTTP_CACHE_DIR);

    if (CommonUtils.TEST_CASE && urlString == null) {
      return null;
    }
    DiskLruCache cache = DiskLruCache.openCache(context, cacheDir, HTTP_CACHE_SIZE);
    // #273 additional checks
    if (cache == null) {
      CommonUtils.debug(TAG, "Failed to open http cache %1$s", cacheDir.getAbsolutePath());
      TrackerUtils.trackBackgroundEvent("httpCacheOpenFail", cacheDir.getAbsolutePath());
      // cache open may fail if there are not enough free space.
      // application will try to clear that cache dir and open cache again
      DiskLruCache.clearCache(context, HTTP_CACHE_DIR);

      // cache clear attempt finished. Let's try again to open cache
      cache = DiskLruCache.openCache(context, cacheDir, HTTP_CACHE_SIZE);
      if (cache == null) {
        CommonUtils.debug(
            TAG, "Failed to open http cache second time %1$s", cacheDir.getAbsolutePath());
        // still unsuccessful. We can't download that bitmap. Let's warn
        // user about this.
        GuiUtils.alert(R.string.errorCouldNotStoreDownloadablePhotoNotEnoughSpace);
        TrackerUtils.trackBackgroundEvent("httpCacheSecondOpenFail", cacheDir.getAbsolutePath());
        return null;
      }
    }

    final File cacheFile = new File(cache.createFilePath(urlString));

    if (cache.containsKey(urlString)) {
      TrackerUtils.trackBackgroundEvent("httpCachHit", TAG);
      if (BuildConfig.DEBUG) {
        Log.d(TAG, "downloadBitmap - found in http cache - " + urlString);
      }
      return cacheFile;
    }
    if (!CommonUtils.checkLoggedInAndOnline(true)) {
      return null;
    }
    if (BuildConfig.DEBUG) {
      Log.d(TAG, "downloadBitmap - downloading - " + urlString);
    }

    Utils.disableConnectionReuseIfNecessary();
    HttpURLConnection urlConnection = null;
    BufferedOutputStream out = null;

    try {
      long start = System.currentTimeMillis();
      final URL url = new URL(urlString);
      urlConnection = (HttpURLConnection) url.openConnection();
      final InputStream in =
          new BufferedInputStream(urlConnection.getInputStream(), Utils.IO_BUFFER_SIZE);
      out = new BufferedOutputStream(new FileOutputStream(cacheFile), Utils.IO_BUFFER_SIZE);

      int b;
      while ((b = in.read()) != -1) {
        out.write(b);
      }
      TrackerUtils.trackDataLoadTiming(System.currentTimeMillis() - start, "downloadBitmap", TAG);
      return cacheFile;

    } catch (final IOException e) {
      GuiUtils.noAlertError(TAG, "Error in downloadBitmap", e);
    } finally {
      if (urlConnection != null) {
        urlConnection.disconnect();
      }
      if (out != null) {
        try {
          out.close();
        } catch (final IOException e) {
          GuiUtils.noAlertError(TAG, "Error in downloadBitmap", e);
        }
      }
    }

    return null;
  }