/** * 清除缓存 */ private void clearCache() { // 清理新闻json数据 - 不清理json数据 // NewsDALManager.shared.clearCache(); // Fresco清除图片缓存 ImagePipeline imagePipeline = Fresco.getImagePipeline(); imagePipeline.clearCaches(); // 清除缓存目录 - 清除所有缓存目录文件 FileCacheUtils.clearAllCache(mContext); final KProgressHUD hud = ProgressHUD.show(mContext, "正在清理..."); new Handler().postDelayed(new Runnable() { @Override public void run() { hud.dismiss(); ProgressHUD.showInfo(mContext, "清理缓存完成"); } }, 2000); }
void attach(BitmapUpdateListener listener) { mBitmapUpdateListener = listener; mAttachCounter++; if (mAttachCounter != 1) { // this is a secondary attach, ignore it, only updating Bitmap boundaries if needed. Bitmap bitmap = getBitmap(); if (bitmap != null) { listener.onSecondaryAttach(bitmap); } return; } listener.onImageLoadEvent(ImageLoadEvent.ON_LOAD_START); Assertions.assertCondition(mDataSource == null); Assertions.assertCondition(mImageRef == null); // Submit the request ImagePipeline imagePipeline = ImagePipelineFactory.getInstance().getImagePipeline(); mDataSource = imagePipeline.fetchDecodedImage(mImageRequest, RCTImageView.getCallerContext()); mDataSource.subscribe(this, UiThreadImmediateExecutorService.getInstance()); }
@ReactMethod public void queryCache(final ReadableArray uris, final Promise promise) { // perform cache interrogation in async task as disk cache checks are expensive new GuardedAsyncTask<Void, Void>(getReactApplicationContext()) { @Override protected void doInBackgroundGuarded(Void... params) { WritableMap result = Arguments.createMap(); ImagePipeline imagePipeline = Fresco.getImagePipeline(); for (int i = 0; i < uris.size(); i++) { String uriString = uris.getString(i); final Uri uri = Uri.parse(uriString); if (imagePipeline.isInBitmapMemoryCache(uri)) { result.putString(uriString, "memory"); } else if (imagePipeline.isInDiskCacheSync(uri)) { result.putString(uriString, "disk"); } } promise.resolve(result); } }.executeOnExecutor(GuardedAsyncTask.THREAD_POOL_EXECUTOR); }
/** * 清除缓存 */ private void clearCache() { // 清理新闻json数据 - 不清理json数据 // NewsDALManager.shared.clearCache(); // Fresco清除图片缓存 ImagePipeline imagePipeline = Fresco.getImagePipeline(); imagePipeline.clearCaches(); // 清除缓存目录 - 清除所有缓存目录文件 FileCacheUtils.clearAllCache(mContext); final KProgressHUD hud = ProgressHUD.show(mContext, "正在清理..."); new Handler().postDelayed(new Runnable() { @Override public void run() { hud.dismiss(); ProgressHUD.showInfo(mContext, "清理缓存完成"); cacheTextView.setText(FileCacheUtils.getTotalCacheSize(mContext)); } }, 2000); }
public void setImage(String uri) { if (uri == null) { iconBitmapDescriptor = null; update(); } else if (uri.startsWith("http://") || uri.startsWith("https://") || uri.startsWith("file://")) { ImageRequest imageRequest = ImageRequestBuilder .newBuilderWithSource(Uri.parse(uri)) .build(); ImagePipeline imagePipeline = Fresco.getImagePipeline(); dataSource = imagePipeline.fetchDecodedImage(imageRequest, this); DraweeController controller = Fresco.newDraweeControllerBuilder() .setImageRequest(imageRequest) .setControllerListener(mLogoControllerListener) .setOldController(logoHolder.getController()) .build(); logoHolder.setController(controller); } else { iconBitmapDescriptor = getBitmapDescriptorByName(uri); update(); } }
public static void loadBitmapFromUrl(Context context, String url, String cookie, String referer, BaseBitmapDataSubscriber dataSubscriber) { if (TextUtils.isEmpty(url)) return; Uri uri = Uri.parse(url); JsonObject header = new JsonObject(); header.addProperty("Cookie", cookie); header.addProperty("Referer", referer); if (HProxy.isEnabled() && HProxy.isAllowPicture()) { HProxy proxy = new HProxy(url); header.addProperty(proxy.getHeaderKey(), proxy.getHeaderValue()); } MyOkHttpNetworkFetcher.headers.put(uri, getGson().toJson(header)); ImagePipeline imagePipeline = Fresco.getImagePipeline(); ImageRequestBuilder builder = ImageRequestBuilder.newBuilderWithSource(uri); ImageRequest request = builder.build(); DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(request, context); dataSource.subscribe(dataSubscriber, CallerThreadExecutor.getInstance()); }
public static void loadResourceFromUrl(Context context, Uri uri, String cookie, String referer, BaseDataSubscriber dataSubscriber) { if (uri.getScheme().startsWith("http")) { JsonObject header = new JsonObject(); header.addProperty("Cookie", cookie); header.addProperty("Referer", referer); if (HProxy.isEnabled() && HProxy.isAllowPicture()) { HProxy proxy = new HProxy(uri.toString()); header.addProperty(proxy.getHeaderKey(), proxy.getHeaderValue()); } MyOkHttpNetworkFetcher.headers.put(uri, getGson().toJson(header)); } ImagePipeline imagePipeline = Fresco.getImagePipeline(); ImageRequestBuilder builder = ImageRequestBuilder.newBuilderWithSource(uri); ImageRequest request = builder.build(); DataSource<CloseableReference<PooledByteBuffer>> dataSource = imagePipeline.fetchEncodedImage(request, context); dataSource.subscribe(dataSubscriber, CallerThreadExecutor.getInstance()); }
@Override public void setViewListener() { settingsListAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() { @Override public void onItemClick(View view, int i) { if (i == 1) { ImagePipeline imagePipeline = Fresco.getImagePipeline(); imagePipeline.clearMemoryCaches(); imagePipeline.clearDiskCaches(); // combines above two lines imagePipeline.clearCaches(); processExtraData(); } } }); }
public static void LoadImageFromURLAndCallBack(SimpleDraweeView destImageView , String URL,Context context,BaseBitmapDataSubscriber bbds) { int w = destImageView.getWidth(); int h =destImageView.getHeight(); if(w<1){ w = destImageView.getLayoutParams().width; } if(h<1){ h =destImageView.getLayoutParams().height; } ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse(URL)) .setResizeOptions(new ResizeOptions(w,h)) .setProgressiveRenderingEnabled(true) .build(); ImagePipeline imagePipeline = Fresco.getImagePipeline(); DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, context); dataSource.subscribe(bbds, CallerThreadExecutor.getInstance()); DraweeController draweeController = Fresco.newDraweeControllerBuilder() .setImageRequest(imageRequest) .setOldController(destImageView.getController()) .setAutoPlayAnimations(true) .build(); destImageView.setController(draweeController); }
public static void LoadImageFromURIAndCallBack(SimpleDraweeView destImageView , Uri uri,Context context,BaseBitmapDataSubscriber bbds) { int w = destImageView.getWidth(); int h =destImageView.getHeight(); if(w<1){ w = destImageView.getLayoutParams().width; } if(h<1){ h =destImageView.getLayoutParams().height; } ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(uri) .setResizeOptions(new ResizeOptions(w,h)) .setProgressiveRenderingEnabled(true) .build(); ImagePipeline imagePipeline = Fresco.getImagePipeline(); DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, context); dataSource.subscribe(bbds, CallerThreadExecutor.getInstance()); DraweeController draweeController = Fresco.newDraweeControllerBuilder() .setImageRequest(imageRequest) .setOldController(destImageView.getController()) .setAutoPlayAnimations(true) .build(); destImageView.setController(draweeController); }
public PipelineDraweeControllerBuilder( Context context, PipelineDraweeControllerFactory pipelineDraweeControllerFactory, ImagePipeline imagePipeline, Set<ControllerListener> boundControllerListeners) { super(context, boundControllerListeners); mImagePipeline = imagePipeline; mPipelineDraweeControllerFactory = pipelineDraweeControllerFactory; }
@Override public void clear(Context ctx, FrescoImageConfig config) { Fresco.getImagePipeline().clearMemoryCaches(); Fresco.getImagePipeline().clearCaches(); Fresco.getImagePipeline().clearDiskCaches(); String url = appendUrl(config.getUrl()); ImagePipeline imagePipeline = Fresco.getImagePipeline(); Uri uri = Uri.parse(url); imagePipeline.evictFromMemoryCache(uri); }
/** * 清除缓存中的一条url * * @param uri 需要被清除的url */ public void evictUri(Uri uri) { ImagePipeline imagePipeline = Fresco.getImagePipeline(); imagePipeline.evictFromMemoryCache(uri); imagePipeline.evictFromDiskCache(uri); imagePipeline.evictFromCache(uri); }
/** * 清除所有缓存 */ public void clear() { ImagePipeline imagePipeline = Fresco.getImagePipeline(); imagePipeline.clearMemoryCaches(); imagePipeline.clearDiskCaches(); imagePipeline.clearCaches(); }
public static void save(Uri uri, String action) { ImageRequest request = ImageRequest.fromUri(uri); ImagePipeline pipeline = Fresco.getImagePipeline(); DataSource<CloseableReference<PooledByteBuffer>> dataSource = pipeline.fetchEncodedImage(request, null); ImageSubscriber dataSubscriber = new ImageSubscriber(uri, action); dataSource.subscribe(dataSubscriber, ExpressApplication.getApplication().getImagePipelineConfig() .getExecutorSupplier().forLocalStorageWrite()); }
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { LogHelper.i("MainActivityFragment", "onActivityResult"); super.onActivityResult(requestCode, resultCode, data); // retrieve selected image Uri from image picker Uri newImageUri = null; if (null != data) { newImageUri = data.getData(); } if (requestCode == TransistorKeys.REQUEST_LOAD_IMAGE && resultCode == Activity.RESULT_OK && newImageUri != null) { ImageHelper imageHelper = new ImageHelper(newImageUri, mActivity, 500, 500); Bitmap newImage = imageHelper.getInputImage(); if (newImage != null && mTempStationID_Position != -1) { // write image to storage File stationImageFile = mTempStation.getStationImageFileReference(mFolder);//get station file with correct path according to UniqueID of the station try (FileOutputStream out = new FileOutputStream(stationImageFile)) { newImage.compress(Bitmap.CompressFormat.PNG, 100, out); out.close(); //remve image from fresco cache ImagePipeline imagePipeline = Fresco.getImagePipeline(); imagePipeline.evictFromCache(Uri.parse(stationImageFile.toURI().toString())); } catch (IOException e) { LogHelper.e(LOG_TAG, "Unable to save: " + newImage.toString()); } // update adapter mCollectionAdapter.notifyItemChanged(mTempStationID_Position); Toast.makeText(mApplication, "Image Updated", Toast.LENGTH_SHORT).show(); } else { LogHelper.e(LOG_TAG, "Unable to get image from media picker. Uri was: " + newImageUri.toString()); } } else { LogHelper.e(LOG_TAG, "Unable to get image from media picker. Did not receive an Uri"); } }
/** * 加载远程图片 * * @param url * @param imageSize */ private void displayImage(Uri url, ResizeOptions imageSize, final ImageView imageView, final DraweeHolder<GenericDraweeHierarchy> draweeHolder) { ImageRequest imageRequest = ImageRequestBuilder .newBuilderWithSource(url) .setResizeOptions(imageSize)//图片目标大小 .build(); ImagePipeline imagePipeline = Fresco.getImagePipeline(); final DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, this); DraweeController controller = Fresco.newDraweeControllerBuilder() .setOldController(draweeHolder.getController()) .setImageRequest(imageRequest) .setControllerListener(new BaseControllerListener<ImageInfo>() { @Override public void onFinalImageSet(String s, ImageInfo imageInfo, Animatable animatable) { CloseableReference<CloseableImage> imageReference = null; try { imageReference = dataSource.getResult(); if (imageReference != null) { CloseableImage image = imageReference.get(); if (image != null && image instanceof CloseableStaticBitmap) { CloseableStaticBitmap closeableStaticBitmap = (CloseableStaticBitmap) image; Bitmap bitmap = closeableStaticBitmap.getUnderlyingBitmap(); if (bitmap != null && imageView != null) { imageView.setImageBitmap(bitmap); } } } } finally { dataSource.close(); CloseableReference.closeSafely(imageReference); } } }) .setTapToRetryEnabled(true) .build(); draweeHolder.setController(controller); }
/** * 清除单张图片的磁盘缓存 * @param url */ public static void clearCacheByUrl(String url){ ImagePipeline imagePipeline = Fresco.getImagePipeline(); Uri uri = Uri.parse(url); // imagePipeline.evictFromMemoryCache(uri); imagePipeline.evictFromDiskCache(uri); //imagePipeline.evictFromCache(uri);//这个包含了从内存移除和从硬盘移除 }
public void setImageUri(String url) { ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse(url)).build(); ImagePipeline imagePipeline = Fresco.getImagePipeline(); final DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, this); DraweeController controller = Fresco.newDraweeControllerBuilder() .setOldController(mDraweeHolder.getController()) .setImageRequest(imageRequest) .setControllerListener(new BaseControllerListener<ImageInfo>() { @Override public void onFinalImageSet(String s, @Nullable ImageInfo imageInfo, @Nullable Animatable animatable) { try { imageReference = dataSource.getResult(); if (imageReference != null) { CloseableImage image = imageReference.get(); // do something with the image if (image != null && image instanceof CloseableStaticBitmap) { CloseableStaticBitmap closeableStaticBitmap = (CloseableStaticBitmap) image; Bitmap bitmap = closeableStaticBitmap.getUnderlyingBitmap(); if (bitmap != null) { setImageBitmap(bitmap); } } } } finally { dataSource.close(); CloseableReference.closeSafely(imageReference); } } }) .setTapToRetryEnabled(true) .build(); mDraweeHolder.setController(controller); }
public void setImageUri(String uri, int width, int height) { ImageRequest imageRequest = ImageRequestBuilder.newBuilderWithSource(Uri.parse(uri)) .setAutoRotateEnabled(true) .setResizeOptions(new ResizeOptions(width, height)) .build(); ImagePipeline imagePipeline = Fresco.getImagePipeline(); final DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, this); DraweeController controller = Fresco.newDraweeControllerBuilder() .setOldController(mDraweeHolder.getController()) .setImageRequest(imageRequest) .setControllerListener(new BaseControllerListener<ImageInfo>() { @Override public void onFinalImageSet(String s, @Nullable ImageInfo imageInfo, @Nullable Animatable animatable) { try { imageReference = dataSource.getResult(); if (imageReference != null) { CloseableImage image = imageReference.get(); if (image != null && image instanceof CloseableStaticBitmap) { CloseableStaticBitmap closeableStaticBitmap = (CloseableStaticBitmap) image; Bitmap bitmap = closeableStaticBitmap.getUnderlyingBitmap(); if (bitmap != null) { setImageBitmap(bitmap); } } } } finally { dataSource.close(); CloseableReference.closeSafely(imageReference); } } }) .setTapToRetryEnabled(true) .build(); mDraweeHolder.setController(controller); }
/** * 清除单张图片的磁盘缓存 * @param url */ public static void clearCacheByUrl(String url){ url = append(url); ImagePipeline imagePipeline = Fresco.getImagePipeline(); Uri uri = Uri.parse(url); // imagePipeline.evictFromMemoryCache(uri); imagePipeline.evictFromDiskCache(uri); //imagePipeline.evictFromCache(uri);//这个包含了从内存移除和从硬盘移除 }
@Override public void onLowMemory() { super.onLowMemory(); ImagePipeline imagePipeline = Fresco.getImagePipeline(); //清空内存缓存(包括Bitmap缓存和未解码图片的缓存) imagePipeline.clearMemoryCaches(); }
@Override public void loadImage(int requestId, Uri uri, final Callback callback) { ImageRequest request = ImageRequest.fromUri(uri); File localCache = getCacheFile(request); if (localCache.exists()) { callback.onCacheHit(localCache); callback.onSuccess(localCache); } else { callback.onStart(); // ensure `onStart` is called before `onProgress` and `onFinish` callback.onProgress(0); // show 0 progress immediately ImagePipeline pipeline = Fresco.getImagePipeline(); DataSource<CloseableReference<PooledByteBuffer>> source = pipeline.fetchEncodedImage(request, true); source.subscribe(new ImageDownloadSubscriber(mAppContext) { @Override protected void onProgress(int progress) { callback.onProgress(progress); } @Override protected void onSuccess(final File image) { callback.onFinish(); callback.onCacheMiss(image); callback.onSuccess(image); } @Override protected void onFail(final Throwable t) { t.printStackTrace(); callback.onFail((Exception) t); } }, mExecutorSupplier.forBackgroundTasks()); closeSource(requestId); saveSource(requestId, source); } }
public static void getBitmapWithFresco(Context context, String url, BaseBitmapDataSubscriber baseBitmapDataSubscriber) { ImageRequest imageRequest = ImageRequestBuilder .newBuilderWithSource(Uri.parse(url)) .setProgressiveRenderingEnabled(true) .build(); ImagePipeline imagePipeline = Fresco.getImagePipeline(); DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, context); dataSource.subscribe(baseBitmapDataSubscriber, CallerThreadExecutor.getInstance()); }
/** * 图片是否已经存在了 */ public static boolean isCached(Context context, Uri uri) { ImagePipeline imagePipeline = Fresco.getImagePipeline(); DataSource<Boolean> dataSource = imagePipeline.isInDiskCache(uri); if (dataSource == null) { return false; } ImageRequest imageRequest = ImageRequest.fromUri(uri); CacheKey cacheKey = DefaultCacheKeyFactory.getInstance() .getEncodedCacheKey(imageRequest, context); BinaryResource resource = ImagePipelineFactory.getInstance() .getMainFileCache().getResource(cacheKey); return resource != null && dataSource.getResult() != null && dataSource.getResult(); }
@Override public void prefetchToDiskCache(Uri uri) { ImageRequestBuilder builder = ImageRequestBuilder .newBuilderWithSource(uri); ImagePipeline pipeline = Fresco.getImagePipeline(); pipeline.prefetchToDiskCache(builder.build(), null); }
@Override public void prefetchToMemoryCache(Uri uri) { ImageRequestBuilder builder = ImageRequestBuilder .newBuilderWithSource(uri); ImagePipeline pipeline = Fresco.getImagePipeline(); pipeline.prefetchToBitmapCache(builder.build(), null); }
@Override public void loadImage(Uri uri, final Callback callback) { ImageRequest request = ImageRequest.fromUri(uri); File localCache = getCacheFile(request); if (localCache.exists()) { callback.onCacheHit(localCache); } else { callback.onStart(); // ensure `onStart` is called before `onProgress` and `onFinish` callback.onProgress(0); // show 0 progress immediately ImagePipeline pipeline = Fresco.getImagePipeline(); DataSource<CloseableReference<PooledByteBuffer>> source = pipeline.fetchEncodedImage(request, true); source.subscribe(new ImageDownloadSubscriber(mAppContext) { @Override protected void onProgress(int progress) { callback.onProgress(progress); } @Override protected void onSuccess(File image) { callback.onFinish(); callback.onCacheMiss(image); } @Override protected void onFail(Throwable t) { // TODO: 12/11/2016 fail t.printStackTrace(); } }, mExecutorSupplier.forBackgroundTasks()); } }
public static void LoadImageFromURLAndCallBack(SimpleDraweeView destImageView , String URL, Context context, BaseBitmapDataSubscriber bbds , BasePostprocessor postprocessor) { int w = destImageView.getWidth(); int h =destImageView.getHeight(); if(w<1){ w = destImageView.getLayoutParams().width; } if(h<1){ h =destImageView.getLayoutParams().height; } ImageRequestBuilder builder = ImageRequestBuilder.newBuilderWithSource(Uri.parse(URL)) .setResizeOptions(new ResizeOptions(w,h)) .setProgressiveRenderingEnabled(true); if(postprocessor!=null){ builder.setPostprocessor(postprocessor); } ImageRequest imageRequest = builder .build(); ImagePipeline imagePipeline = Fresco.getImagePipeline(); DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, context); dataSource.subscribe(bbds, CallerThreadExecutor.getInstance()); DraweeController draweeController = Fresco.newDraweeControllerBuilder() .setImageRequest(imageRequest) .setOldController(destImageView.getController()) .setAutoPlayAnimations(true) .build(); destImageView.setController(draweeController); }
/** * 预加载该页的图片, 用于分享 */ private void prefetchToBitmapCache(Uri uri){ ImagePipeline imagePipeline = Fresco.getImagePipeline(); ImageRequest imageRequest = ImageRequestBuilder .newBuilderWithSource(uri) .build(); imagePipeline.prefetchToBitmapCache(imageRequest, this); }
@Override public View instantiateItem(ViewGroup container, int position) { View view = LayoutInflater.from(ImageViewActivity.this).inflate(R.layout.item_imagepage, container, false); final PhotoView photoView = (PhotoView) view.findViewById(R.id.photoview); final View wheel = view.findViewById(R.id.wheel); photoView.setOnPhotoTapListener((view1, v, v1) -> finish()); ImagePipeline imagePipeline = Fresco.getImagePipeline(); ImageRequest request = ImageRequestBuilder.newBuilderWithSource(urls.get(position)) .setResizeOptions(new ResizeOptions(768, 768)) .build(); DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(request,this); DataSubscriber dataSubscriber = new BaseBitmapDataSubscriber() { @Override protected void onNewResultImpl(Bitmap bitmap) { photoView.setImageBitmap(bitmap); wheel.setVisibility(View.GONE); } @Override protected void onFailureImpl(DataSource<CloseableReference<CloseableImage>> closeableReferenceDataSource) { } }; dataSource.subscribe(dataSubscriber, new Executor() { @Override public void execute(Runnable command) { handler.post(command); } }); container.addView(view); return view; }
private void cacheImage(String url) { if (!isImageDownloaded(Uri.parse(url))) { ImagePipeline imagePipeline = Fresco.getImagePipeline(); ImageRequest request = ImageRequest.fromUri(url); imagePipeline.prefetchToDiskCache(request, this); offlinePictureLength += request.getSourceFile().length(); offlinePictureCount++; } }