public Bitmap getBitmap(String fileId, String size, boolean fromUrl) { File file = fileCache.getFile(fileId + size); // from SD cache Bitmap bitmap = decodeFile(file, size); if (bitmap != null) { return bitmap; } // from web try { bitmap = null; if (fromUrl) { InputStream is = ConnectionHandler.httpGetRequest(fileId, UsersManagement.getLoginUser().getId()); OutputStream os = new FileOutputStream(file); Utils.copyStream(is, os); os.close(); is.close(); } else { CouchDB.downloadFile(fileId, file); } bitmap = decodeFile(file, size); return bitmap; // return ConnectionHandler.getBitmapObject(url); } catch (Throwable ex) { ex.printStackTrace(); if (ex instanceof OutOfMemoryError) memoryCache.clear(); return null; } }
public int read(byte[] b, int off, int len) throws IOException { checkClosed(); if (b == null) { throw new NullPointerException("b == null!"); } if (off < 0 || len < 0 || off + len > b.length || off + len < 0) { throw new IndexOutOfBoundsException( "off < 0 || len < 0 || off+len > b.length || off+len < 0!"); } bitOffset = 0; if (len == 0) { return 0; } long pos = cache.loadFromStream(stream, streamPos + len); len = (int) (pos - streamPos); // In case stream ended early if (len > 0) { cache.read(b, off, len, streamPos); streamPos += len; return len; } else { return -1; } }
/** * Launch async runnable to show this image * * @param pictureRowId The picture rowId * @param thumbnailPath The thumbnail path * @param fullPicturePath The full file image path * @param imageView The imageView to put the image */ public void DisplayImage( ID_TYPE pictureRowId, THUMBNAIL_TYPE thumbnail, FULL_IMAGE_TYPE fullPictuure, ImageView imageView) { // create the object containing all the relevant data PhotoToLoad data = new PhotoToLoad(pictureRowId, thumbnail, fullPictuure, imageView); // store the links imageViews.put(imageView, data); // attempt to access cached full picture Bitmap bitmap = null; if (showFullImage) bitmap = memoryCache.getFullPicture(pictureRowId); // no full picture, so queue the photo loader, and check for thumbnail if (bitmap == null) { bitmap = memoryCache.getThumbnail(pictureRowId); if (bitmap == null) queuePhoto(data, true); else if (showFullImage) queuePhoto(data, false); } // see if we have a bitmap to access if (bitmap != null) imageView.setImageBitmap(bitmap); // otherwise just show the default image else imageView.setImageResource(stub_id); }
/** * @param placeNameServiceSet the set of PlaceNameService objects that PlaceNameLayer will render. * @throws IllegalArgumentException if {@link * gov.nasa.worldwind.layers.placename.PlaceNameServiceSet} is null */ public PlaceNameLayer(PlaceNameServiceSet placeNameServiceSet) { if (placeNameServiceSet == null) { String message = Logging.getMessage("nullValue.PlaceNameServiceSetIsNull"); Logging.logger().fine(message); throw new IllegalArgumentException(message); } // this.placeNameServiceSet = placeNameServiceSet.deepCopy(); for (int i = 0; i < this.placeNameServiceSet.getServiceCount(); i++) { // todo do this for long as well and pick min int calc1 = (int) (PlaceNameService.TILING_SECTOR.getDeltaLatDegrees() / this.placeNameServiceSet .getService(i) .getTileDelta() .getLatitude() .getDegrees()); int numLevels = (int) Math.log(calc1); navTiles.add( new NavigationTile( this.placeNameServiceSet.getService(i), PlaceNameService.TILING_SECTOR, numLevels, "top")); } if (!WorldWind.getMemoryCacheSet().containsCache(Tile.class.getName())) { long size = Configuration.getLongValue(AVKey.PLACENAME_LAYER_CACHE_SIZE, 2000000L); MemoryCache cache = new BasicMemoryCache((long) (0.85 * size), size); cache.setName("Placename Tiles"); WorldWind.getMemoryCacheSet().addCache(Tile.class.getName(), cache); } }
public int read() throws IOException { checkClosed(); bitOffset = 0; long pos = cache.loadFromStream(stream, streamPos + 1); if (pos >= streamPos + 1) { return cache.read(streamPos++); } else { return -1; } }
public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_website: Uri uri = Uri.parse("http://moeapk.com/apk/" + app_package + ".html"); Intent intent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent); break; case R.id.menu_clear_cache: memoryCache.clear(); fileUtils.clearCache(); Toast.makeText( getApplicationContext(), R.string.toast_clear_cache_finish, Toast.LENGTH_LONG) .show(); break; case R.id.menu_about: Intent myintent = new Intent(); myintent.setClass(ApkListActivity.this, AboutActivity.class); startActivity(myintent); break; case R.id.menu_exit: ActivityManager.getInstance().exit(); break; } return true; }
private Bitmap getBitmap(String url, int requiredSize) { File f = fileCache.getFile(url); Bitmap b = decodeFile(f, requiredSize); if (b != null) return b; Utils.logDebug(TAG, "load image from " + url); Bitmap bitmap = null; InputStream is = null; OutputStream os = null; try { HttpTransport httpTransport = new HttpTransport(); is = httpTransport.executeGetRequest(url); os = new FileOutputStream(f); Utils.copyStream(is, os); bitmap = decodeFile(f, requiredSize); } catch (Throwable ex) { ex.printStackTrace(); if (ex instanceof OutOfMemoryError) memoryCache.clear(); } finally { if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } } return bitmap; }
// for setting image as a source public void DisplayImageAsSource(String url, ImageView imageView) { if (null != url) url.replace("\\", ""); Log.e("URL", url); // Store image and url in Map imageViews.put(imageView, url); // Check image is stored in MemoryCache Map or not (see MemoryCache.java) Bitmap bitmap = memoryCache.get(url); if (bitmap != null) { // if image is stored in MemoryCache Map then // Show image in listview row BitmapDrawable bitmapDrawable = new BitmapDrawable(null, bitmap); Drawable drawable = (Drawable) bitmapDrawable; imageView.setImageDrawable(drawable); } else { // queue Photo to download from url queuePhoto(url, imageView); // Before downloading image show default image // imageView.setImageResource(stub_id); } }
// for setting image as a background @SuppressLint("NewApi") public void DisplayImage(String url, ImageView imageView) { if (null != url) url.replace("\\", ""); Log.e("URL", url); // Store image and url in Map imageViews.put(imageView, url); // Check image is stored in MemoryCache Map or not (see MemoryCache.java) Bitmap bitmap = memoryCache.get(url); if (bitmap != null) { // if image is stored in MemoryCache Map then // Show image in listview row BitmapDrawable bitmapDrawable = new BitmapDrawable(null, bitmap); Drawable drawable = (Drawable) bitmapDrawable; if (Build.VERSION.SDK_INT >= 4.0) { imageView.setBackgroundDrawable(drawable); // this code will be executed on devices running on DONUT (NOT ICS) or later } else imageView.setBackground(drawable); // imageView.setImageBitmap(bitmap); } else { // queue Photo to download from url queuePhoto(url, imageView); // Before downloading image show default image // imageView.setImageResource(stub_id); } }
public Bitmap getBitmap(String url) { File f = fileCache.getFile(url); // from SD cache Bitmap bitmap = decodeFile(f); if (bitmap != null) { Logger.error("ImageLoader", "fileCache.returnBitmap : " + url); return bitmap; } // from web try { bitmap = null; InputStream is = ConnectionHandler.httpGetRequest(url, UsersManagement.getLoginUser().getId()); OutputStream os = new FileOutputStream(f); Utils.copyStream(is, os); os.close(); is.close(); // conn.disconnect(); bitmap = decodeFile(f); return bitmap; // return ConnectionHandler.getBitmapObject(url); } catch (Throwable ex) { ex.printStackTrace(); if (ex instanceof OutOfMemoryError) memoryCache.clear(); return null; } }
@Override public Boolean loadInBackground() { boolean flag = false; // 进行开启下载任务 try { byte[] bs = webCache.getByteFromInternet(url); if (bs != null) { String json = new String(bs, "utf-8"); List<News> list = JsonUtils.turnToJson(json); for (News news : list) { // 将list加入数据库中 newshander.insertToSQL(news); // 下载图片 String url = news.getLitpic(); byte[] bytes = webCache.getByteFromInternet(url); // 保存图片 Bitmap compress = Compress.ImageCompress(bytes, 60, 60); String filename = fileCache.addToSdcard(url, BitmapToByte.bitmapToByte(compress)); newshander.updata(url, filename); memoryCache.addtoCache(filename, compress); } } } catch (Exception e) { e.printStackTrace(); } return flag; }
private Bitmap getBitmap(String url) { File f = fileCache.getFile(url); // from SD cache Bitmap b = decodeFile(f); if (b != null) return b; // from web try { Bitmap bitmap = null; URL imageUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection(); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.setInstanceFollowRedirects(true); InputStream is = conn.getInputStream(); OutputStream os = new FileOutputStream(f); Utils.CopyStream(is, os); os.close(); conn.disconnect(); bitmap = decodeFile(f); return bitmap; } catch (Throwable ex) { ex.printStackTrace(); if (ex instanceof OutOfMemoryError) memoryCache.clear(); return null; } }
/** * 最主要的方法 * * @param url * @param imageView * @param requiredSize 裁剪图片大小尺寸(一直裁剪到图片宽或高 至少有一个小与requiredSize的时候) * @param listener * @param defaultPicResId */ public void displayImage( String url, ImageView imageView, int requiredSize, OnImageLoaderListener listener, int defaultPicResId) { imageViews.put(imageView, url); // 先从内存缓存中查找 Bitmap bitmap = memoryCache.get(url); if (bitmap != null) { imageView.setImageBitmap(bitmap); if (null != listener) { listener.onFinishedImageLoader(imageView, bitmap); // 通知完成加载 } } else { // 如果defaultPicResId小于0,则不设置默认图片 if (defaultPicResId < 0) { queuePhoto(url, imageView, requiredSize, listener); return; } /** * 如果defaultPicResId等于0,则设置默认图片为config中的默认图片,并开启新线程加载真实需要的图片 * 如果defaultPicResId大于0,则设置默认图片为指定的默认图片,并开启新线程加载真实需要的图片 */ if (defaultPicResId == 0) { defaultPicResId = config.getDefaultResId(); } imageView.setImageResource(defaultPicResId); queuePhoto(url, imageView, requiredSize, listener); } }
public BaseObject getXml(String id, String className, String fileName) { BaseObject xmlObject = memoryCache.getXml(id); if (xmlObject == null) { xmlObject = fileCache.getXml(id); if (xmlObject == null) { // Set default xml. fileCache.setDefaultXml(id, className, fileName); xmlObject = fileCache.getXml(id); } if (xmlObject != null) { memoryCache.setXml(id, xmlObject); } } return xmlObject; }
public void DisplayImage(String url, int loader, ImageView imageView) { defaultResId = loader; imageViews.put(imageView, url); Bitmap bitmap = memoryCache.get(url); if (bitmap != null) imageView.setImageBitmap(bitmap); else { PhotoToLoad photoToLoad = new PhotoToLoad(url, imageView); photoToLoad.imageView.setImageResource(loader); new AsyncTask<PhotoToLoad, Void, Bitmap>() { private PhotoToLoad pt; @Override protected Bitmap doInBackground(PhotoToLoad... params) { pt = params[0]; if (imageViewReused(pt)) return null; Bitmap bitmap = null; // from SD cache File f = fileCache.getFile(pt.url); try { bitmap = BitmapFactory.decodeStream(new FileInputStream(f)); } catch (FileNotFoundException e) { e.printStackTrace(); } if (bitmap != null) return bitmap; // from web try { URL imageUrl = new URL(pt.url); HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection(); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.setInstanceFollowRedirects(true); InputStream is = conn.getInputStream(); OutputStream os = new FileOutputStream(f); Utils.CopyStream(is, os); os.close(); bitmap = BitmapFactory.decodeStream(new FileInputStream(f)); return bitmap; } catch (Exception ex) { ex.printStackTrace(); return null; } } @Override protected void onPostExecute(Bitmap bitmap) { memoryCache.put(pt.url, bitmap); if (imageViewReused(pt)) return; BitmapDisplayer bd = new BitmapDisplayer(bitmap, pt); Activity a = (Activity) pt.imageView.getContext(); a.runOnUiThread(bd); } }.execute(photoToLoad); } }
/** * Called to add a new image to the memory cache * * @param data The key identifier * @param bitmap The {@link Bitmap} to cache */ public void addBitmapToMemCache(final String data, final Bitmap bitmap) { if (data == null || bitmap == null) { return; } // Add to memory cache if (getBitmapFromMemCache(data) == null) { mLruCache.put(data, bitmap); } }
public void DisplayImage(String url, ImageView imageView) { imageViews.put(imageView, url); Bitmap bitmap = memoryCache.get(url); if (bitmap != null) imageView.setImageBitmap(bitmap); else { queuePhoto(url, imageView); imageView.setImageResource(stub_id); } }
public void displayImage(String url, ImageView imageView, ImageType type) { imageViews.put(imageView, url); Bitmap bitmap = memoryCache.get(url); if (bitmap != null) { imageView.setImageBitmap(bitmap); } else { queuePhoto(url, imageView, type, DEFAULT_REQUIRED_SIZE); imageView.setImageResource(getDefaultImageResource(type)); } }
public void DisplayImage(String url, ImageView imageView, boolean roundCorner) { imageViews.put(imageView, url); Bitmap bitmap = memoryCache.get(url); if (bitmap != null) { if (imageView instanceof ImageViewCrop) { ((ImageViewCrop) imageView).setImageBitmap(bitmap); } else imageView.setImageBitmap(bitmap); } else { queuePhoto(url, imageView, roundCorner); } }
public void DisplayImage(String url, ImageView imageView) { // Log.e("DISPLAY iMage----", "msg-"+url); imageViews.put(imageView, url); Bitmap bitmap = memoryCache.get(url); // Log.i("LLLL", bitmap+"<<"+url); if (bitmap != null) imageView.setImageBitmap(bitmap); else { imageView.setImageResource(stub_id); queuePhoto(url, imageView); } }
public BaseObject setXml( final String id, final String xmlString, final Class<? extends BaseObject> c) { BaseObject xmlObject = Util.convertXmlToObject(xmlString, c); if (xmlObject != null) { DbCache dbObject = new DbCache(id, xmlString, c.getName()); fileCache.setXml(dbObject); xmlObject.setLastUpdateDate(dbObject.getDate()); memoryCache.setXml(id, xmlObject); } return xmlObject; }
public void DisplayImage(String url, ImageView imageView, int rotationDegrees) { imageViews.put(imageView, url); Bitmap bitmap = memoryCache.get(url + rotationDegrees); if (bitmap != null) { if (imageView instanceof ImageViewCrop) { ((ImageViewCrop) imageView).setImageBitmap(bitmap); } else imageView.setImageBitmap(bitmap); } else { queuePhoto(url, imageView, rotationDegrees); } }
public void DisplayImage( String fileId, ImageView imageView, String size, boolean downloadFromUrl) { imageViews.put(imageView, fileId + size); Bitmap bitmap = memoryCache.get(fileId + size); if (bitmap != null) { imageView.setImageBitmap(bitmap); } else { queuePhoto(fileId, size, imageView, downloadFromUrl); // imageView.setImageResource(stub_id); } }
/** * Fetches a cached image from the memory cache * * @param data Unique identifier for which item to get * @return The {@link Bitmap} if found in cache, null otherwise */ public final Bitmap getBitmapFromMemCache(final String data) { if (data == null) { return null; } if (mLruCache != null) { final Bitmap lruBitmap = mLruCache.get(data); if (lruBitmap != null) { return lruBitmap; } } return null; }
@Override public void onDownloadComplete( BitmapWorkerTask caller, String url, @Nullable Bitmap result, boolean wasCancelled) { Log.d("ImageLoader", "Download complete: " + url); // When a download is completed, if the task wasn't cancelled the content is queued // to be written to cache. If the task was cancelled, the Bitmap might be corrupt. if (!wasCancelled && result != null) { MemoryCache.instance().addBitmapToMemoryCache(url, result); queueWriteRequest(new WriteRequest(result, url)); } // The request is closed closeRequest(); }
/** * This method is used to load image * * @param url is the url to load image * @param imageView is the image view that you need to load image on * @param hasImageSmall is true if there is a small bitmap on image view before hasImageSmall is * false if there is no a small bitmap on image view before * @param requestWidth is with of image view * @param requestHeight is height of image view * @param typeImage is the type of image that you want to load. There are four types TYPE_NORMAL * load image normal */ public void DisplayImage(String url, ImageView imageView, int requestWidth, int requestHeight) { imageViews.put(imageView, url); Bitmap bitmap = memoryCache.get(url); if (bitmap != null) { imageView.setImageBitmap(bitmap); } else { queuePhoto(url, imageView, requestWidth, requestHeight); try { imageView.setImageResource(R.drawable.icon_book_big); } catch (Exception e) { e.printStackTrace(); } } }
public void DisplayImage( String fileId, ImageView imageView, ProgressBar pbLoading, String size, boolean downloadFromUrl) { imageViews.put(imageView, fileId + size); Bitmap bitmap = memoryCache.get(fileId + size); if (bitmap != null) { imageView.setImageBitmap(bitmap); pbLoading.setVisibility(View.GONE); } else { queuePhotoWithProgress(fileId, size, imageView, pbLoading, downloadFromUrl); } }
private Bitmap getBitmap(String url) { // File f=fileCache.getFile(url); File f = fileCache.getFile(url); // from SD cache // CHECK : if trying to decode file which not exist in cache return null Bitmap b = decodeFile(f); if (b != null) return b; // Download image file from web try { System.out.println("Downloading image::" + url); Bitmap bitmap = null; URL imageUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection(); conn.setConnectTimeout(30000); conn.setReadTimeout(30000); conn.setInstanceFollowRedirects(true); InputStream is = conn.getInputStream(); // Constructs a new FileOutputStream that writes to file // if file not exist then it will create file OutputStream os = new FileOutputStream(f); // See Utils class CopyStream method // It will each pixel from input stream and // write pixels to output stream (file) Utils.CopyStream(is, os); os.close(); conn.disconnect(); // Now file created and going to resize file with defined height // Decodes image and scales it to reduce memory consumption bitmap = decodeFile(f); return bitmap; } catch (Throwable ex) { ex.printStackTrace(); if (ex instanceof OutOfMemoryError) memoryCache.clear(); return null; } }
@Override protected void onProgressUpdate(LoadRequest... values) { // For each published request for (LoadRequest request : values) { // The bitmap needs to be downloaded if (request.mResult == null) { // If the download is NOT to be performed if (request.mFlinging) { // The placeholder is set to the view and the request is closed. request.mImageView.setImageBitmap(mPlaceHolderBitmap); closeRequest(); } // If the download is to be performed else if (cancelPotentialWork(request.mUrl, request.mImageView)) { BitmapWorkerTask task = getBitmapWorkerTask(request.mImageView); // If the reference was missed and picked by the GC, the task will // get killed and will never call onDownloadComplete(), so the // request needs to get closed here. if (task != null) { closeRequest(); } // The proper task is created task = new BitmapWorkerTask(mContext, request.mImageView, this); // An AsyncDrawable is created with the selected configuration and set // as the drawable of the ImageView final AsyncDrawable asyncDrawable; if (request.mUsePlaceholder) { asyncDrawable = new AsyncDrawable(mContext.getResources(), mPlaceHolderBitmap, task); } else { asyncDrawable = new AsyncDrawable(mContext.getResources(), task); } request.mImageView.setImageDrawable(asyncDrawable); task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, request.mUrl); } } else { MemoryCache.instance().addBitmapToMemoryCache(request.mUrl, request.mResult); request.mImageView.setImageBitmap(request.mResult); closeRequest(); } } }
/** @param key The key used to identify which cache entries to delete. */ public void removeFromCache(final String key) { if (key == null) { return; } // Remove the Lru entry if (mLruCache != null) { mLruCache.remove(key); } try { // Remove the disk entry if (mDiskCache != null) { mDiskCache.remove(hashKeyForDisk(key)); } } catch (final IOException e) { Log.e(TAG, "remove - " + e); } flush(); }