/** * 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; }
@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; }