我是靠谱客的博主 热心奇异果,这篇文章主要介绍分享Fresco缓存中的图片,现在分享给大家,希望可以做个参考。

由于使用Fresco框架加载网络图片,然后又面临要分享图片的需求,于是研究了下如何使用分享fresco缓存下的图片。

已经确定缓存中有的图片:

先说思路,本来是想直接分享缓存路径中的图片缓存的,但是Fresco缓存的格式是.cnt,不能直接分享,于是通过fresco本身自带的功能获取到图片的bitmap,在将bitmap存到SD卡上,再按照sd卡上的路径进行分享。
具体实现如下:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//从缓存中获取图片的bitmap ImageRequest imageRequest=ImageRequestBuilder.newBuilderWithSource(Uri.parse(url)) .setResizeOptions(resizeOptions) .setAutoRotateEnabled(true) .build(); ImagePipeline imagePipeline = Fresco.getImagePipeline(); DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(imageRequest, this); CloseableReference<CloseableImage> imageCloseableReference = dataSource.getResult(); final String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/share"+"/"+photoFiles.get(position).getName(); if (imageCloseableReference != null) { final CloseableImage image = imageCloseableReference.get(); if (image != null && image instanceof CloseableStaticBitmap) { CloseableStaticBitmap closeableStaticBitmap = (CloseableStaticBitmap) image; Bitmap bitmap = closeableStaticBitmap.getUnderlyingBitmap(); if (bitmap != null) { //把获取的Bitmap转存到SD卡上 File docment = new File(sdPath); try { FileOutputStream fos = new FileOutputStream(docment); bitmap.compress(Bitmap.CompressFormat.JPEG,90,fos); } catch (FileNotFoundException e) { e.printStackTrace(); } File f = new File(sdPath); Uri imagePath = Uri.fromFile(f); //把SD卡路径上的图片分享出去 Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_STREAM, imagePath); shareIntent.setType("image/*"); startActivity(Intent.createChooser(shareIntent, "分享到")); mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, imagePath)); } } }

这样就可以在Fresco的基础上分享图片了。
如果你不确定你的缓存中是否存在这张图片,那么可以使用下面的代码,在图片获取到了之后在进行分享

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
DraweeController controller = Fresco.newDraweeControllerBuilder() .setControllerListener(new BaseControllerListener<ImageInfo>(){ @Override public void onFinalImageSet(String id, ImageInfo imageInfo, Animatable animatable) { super.onFinalImageSet(id, imageInfo, animatable); //1:获取bitmap //2:存sd卡 //3:分享sd的图片 } }) .build(); frescoView.setController(controller);

当然这只是自己摸索出来的方式,确实比较繁琐,如果你知道更好的方式,希望可以指点,交流下!

最后

以上就是热心奇异果最近收集整理的关于分享Fresco缓存中的图片的全部内容,更多相关分享Fresco缓存中内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(74)

评论列表共有 0 条评论

立即
投稿
返回
顶部