示例#1
0
  /**
   * Load the bitmap according form the specified URI.
   *
   * @param uri
   * @return
   */
  private Bitmap loadBitmap(String uri) {
    if (null == uri) {
      return null;
    }

    if (DEBUG) {
      cacheLogD(mIdCache + "  +++ loading new bitmap: " + uri);
    }

    Bitmap btm = null;
    if (uri.startsWith(HEADER_RESOURCE)) {
      String sRes = uri.replaceFirst(HEADER_RESOURCE, "");
      int res;
      try {
        res = Integer.parseInt(sRes);
        return loadBitmap(res);
      } catch (NumberFormatException e) {
        btm = null;
      }
    } else if (uri.startsWith(HEADER_ASSETS)) {
      String path = uri.replace(HEADER_ASSETS, "");
      AssetManager assets = mRes.getAssets();
      try {
        InputStream is = assets.open(path);

        Options options = getOptimizedBitmapOption();
        if (options.inJustDecodeBounds) {
          DebugBitmap.decodeStream(is, null, options, path);
          options.inSampleSize = calculateInSampleSize(options, mMaxWidth, mMaxHeight);
          options.inJustDecodeBounds = false;
        }

        is.close();
        is = assets.open(path);
        btm = DebugBitmap.decodeStream(is, null, options, path);

      } catch (IOException e) {
        e.printStackTrace();
      }

    } else if (uri.startsWith(HEADER_HTTP) || uri.startsWith(HEADER_HTTPS)) {
      btm = requestDownloadBitmap(uri);
    } else if (uri.startsWith(HEADER_FILE) || uri.startsWith(HEADER_FILE_)) {
      String sRes = uri.replaceFirst(HEADER_FILE, "");
      btm = loadImageFromFile(sRes);
    }

    if (null == btm) {
      cacheLogE(mIdCache + " Error loading the resource: " + uri);
      // btm = _defaulResource;
    } else {
      storeBitmap(uri, btm);
    }

    return btm;
  }
示例#2
0
  private Bitmap loadImageFromFile(String file) {
    Options options = getOptimizedBitmapOption();
    if (options.inJustDecodeBounds) {
      BitmapFactory.decodeFile(file, options);
      options.inSampleSize = calculateInSampleSize(options, mMaxWidth, mMaxHeight);
      options.inJustDecodeBounds = false;
    }

    Bitmap btm = DebugBitmap.decodeFile(file, options);
    return btm;
  }
示例#3
0
  private Bitmap loadBitmap(int id) {
    String key = normalizeURI(id);
    Bitmap btm = mBitmapContainer.get(key);
    if (null != btm) {
      return (btm);
    }

    Options options = getOptimizedBitmapOption();
    if (options.inJustDecodeBounds) {
      BitmapFactory.decodeResource(mRes, id, options);
      options.inSampleSize = calculateInSampleSize(options, mMaxWidth, mMaxHeight);
      options.inJustDecodeBounds = false;
    }

    btm = DebugBitmap.decodeResource(mRes, id, options);

    return storeBitmap(key, btm);
  }
示例#4
0
    @Override
    public TaskResult runTask() {
      TaskResult resultDependencies = null;
      long time = System.currentTimeMillis();
      Bitmap btm = null;

      if (DEBUG) {
        cacheLogD(mIdCache + " ||||||||||   Obtaining the image from internet: " + mUri);
      }
      URL url;
      try {
        url = new URL(mUri);
        URLConnection urlConn = url.openConnection();
        urlConn.setConnectTimeout(DEFAULT_TIME_OUT);
        urlConn.setAllowUserInteraction(false);
        urlConn.setDoOutput(true);

        InputStream is = url.openStream(); // urlConn.getInputStream();
        // Big image:
        // http://ettugamer.com/wp-content/gallery/marvel-vs-capcom-3-3/marvel_vs_capcom_3_ironman.png

        Options options = getOptimizedBitmapOption();
        if (options.inJustDecodeBounds) {
          DebugBitmap.decodeStream(is, null, options, mUri);
          options.inSampleSize = calculateInSampleSize(options, mMaxWidth, mMaxHeight);
          options.inJustDecodeBounds = false;
        }

        is.close();
        is = url.openStream();
        btm = DebugBitmap.decodeStream(is, null, options, mUri);

      } catch (MalformedURLException e) {
        String strData = mIdCache + " Error geting the image form internet: " + e;
        cacheLogE(strData);
        resultDependencies =
            new TaskResult(getIdTask(), true, TaskResult.TASK_MESSAGE_ERROR_MAIN_TASK);
      } catch (IOException e) {
        String strData = mIdCache + " Error geting the image form internet: " + e;
        cacheLogE(strData);
        resultDependencies =
            new TaskResult(getIdTask(), true, TaskResult.TASK_MESSAGE_ERROR_MAIN_TASK);
      }
      mBtm = btm;

      if (mBtm != null) {
        storeBitmap(mUri, mBtm);
        cacheLogD(
            mIdCache
                + " Image loaded form internet ("
                + (System.currentTimeMillis() - time)
                + " ms): "
                + mUri);

        if (mOnLoadBitmapListener != null) {
          for (int i = 0; i < mOnLoadBitmapListener.size(); i++) {
            mOnLoadBitmapListener.get(i).onExternalBitmapLoaded(BitmapCache.this, mUri, mBtm);
          }
        }

      } else {
        mLoadingResources.put(mUri, ERROR_LOADING_IMAGE);
      }

      return resultDependencies;
    }