コード例 #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;
    }