Exemplo n.º 1
0
 @Override
 public void onDestroyView() {
   super.onDestroyView();
   if (mAdapter != null) {
     mAdapter.forceStopLoadingIfNecessary();
   }
   GuiUtils.removeGlobalOnLayoutListener(photosGrid, photosGridListener);
 }
Exemplo n.º 2
0
  /**
   * The main process method, which will be called by the ImageWorker in the AsyncTaskEx background
   * thread.
   *
   * @param data The data to load the bitmap, in this case, a regular http URL
   * @param imageWidth
   * @param imageHeight
   * @return The downloaded and resized bitmap
   */
  protected Bitmap processBitmap(String data, int imageWidth, int imageHeight) {
    if (BuildConfig.DEBUG) {
      Log.d(TAG, "processBitmap - " + data);
    }

    // Download a bitmap, write it to a file
    final File f = downloadBitmap(mContext, data);

    if (f != null) {
      try {
        // Return a sampled down version
        return decodeSampledBitmapFromFile(f.toString(), imageWidth, imageHeight, cornerRadius);
      } catch (Exception ex) {
        GuiUtils.error(TAG, ex);
      }
    }

    return null;
  }
Exemplo n.º 3
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;
 }
Exemplo n.º 4
-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;
  }