コード例 #1
0
    @Override
    protected CacheableBitmapDrawable doInBackground(String... params) {
      try {
        // Return early if the ImageView has disappeared.
        if (holder.userId != userId) {
          return null;
        }
        final String url = params[0];

        // Now we're not on the main thread we can check all caches
        CacheableBitmapDrawable result;

        try {
          result = mCache.get(url, null);
        } catch (Exception e) {
          result = null;
        }

        if (null == result) {
          Log.d("ImageUrlAsyncTask", "Downloading: " + url);

          // The bitmap isn't cached so download from the web
          HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
          InputStream is = new BufferedInputStream(conn.getInputStream());

          Bitmap b = decodeSampledBitmapFromResourceMemOpt(is, 500, 500);

          try {
            is.close();
          } catch (Exception e) {

          }
          try {
            conn.disconnect();
          } catch (Exception e) {

          }

          // Add to cache
          try {
            result = mCache.put(url, b);
          } catch (Exception e) {
            result = null;
          }

        } else {
          Log.d("ImageUrlAsyncTask", "Got from Cache: " + url);
        }

        return result;

      } catch (IOException e) {
        Log.e("ImageUrlAsyncTask", e.toString());
      } catch (OutOfMemoryError e) {
        e.printStackTrace();
      }

      return null;
    }
コード例 #2
0
    @Override
    protected CacheableBitmapDrawable doInBackground(String... params) {
      try {
        if (holder.userId != id) {
          return null;
        }
        final String url = params[0];

        // Now we're not on the main thread we can check all caches
        CacheableBitmapDrawable result;

        try {
          result = mCache.get(url, null);
        } catch (Exception e) {
          return null;
        }

        if (null == result) {

          // The bitmap isn't cached so download from the web
          HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
          InputStream is = new BufferedInputStream(conn.getInputStream());

          BitmapFactory.Options options = new BitmapFactory.Options();
          options.inJustDecodeBounds = false;

          Bitmap b = decodeSampledBitmapFromResourceMemOpt(is, 500, 500);

          // Add to cache
          try {
            result = mCache.put(url, b);
          } catch (Exception e) {
            result = null;
          }

          try {
            is.close();
          } catch (Exception e) {

          }
          try {
            conn.disconnect();
          } catch (Exception e) {

          }
        }

        return result;

      } catch (IOException e) {
        Log.e("ImageUrlAsyncTask", e.toString());
      } catch (OutOfMemoryError e) {
        Log.v("ImageUrlAsyncTask", "Out of memory error here");
      }

      return null;
    }
コード例 #3
0
  public void loadImage(
      Context context,
      final ViewHolder holder,
      final String url,
      BitmapLruCache mCache,
      final long tweetId) {
    // First check whether there's already a task running, if so cancel it
    /*if (null != mCurrentTask) {
        mCurrentTask.cancel(true);
    }*/

    if (url == null) {
      return;
    }

    BitmapDrawable wrapper = mCache.getFromMemoryCache(url);

    if (null != wrapper && holder.picture.getVisibility() != View.GONE) {
      // The cache has it, so just display it
      holder.picture.setImageDrawable(wrapper);
      Animation fadeInAnimation = AnimationUtils.loadAnimation(context, R.anim.fade_in);

      holder.picture.startAnimation(fadeInAnimation);
    } else {
      // Memory Cache doesn't have the URL, do threaded request...
      holder.picture.setImageDrawable(null);

      mCurrentTask = new ImageUrlAsyncTask(context, holder, mCache, tweetId);

      try {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
          SDK11.executeOnThreadPool(mCurrentTask, url);
        } else {
          mCurrentTask.execute(url);
        }
      } catch (RejectedExecutionException e) {
        // This shouldn't happen, but might.
      }
    }
  }