-
Notifications
You must be signed in to change notification settings - Fork 17
/
FrescoImageLoader.java
173 lines (150 loc) · 5.95 KB
/
FrescoImageLoader.java
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.awen.photo;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import com.awen.photo.photopick.ui.VideoPlayActivity;
import com.awen.photo.photopick.util.FileSizeUtil;
import com.facebook.binaryresource.BinaryResource;
import com.facebook.binaryresource.FileBinaryResource;
import com.facebook.cache.common.CacheKey;
import com.facebook.common.util.UriUtil;
import com.facebook.datasource.DataSource;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.cache.DefaultCacheKeyFactory;
import com.facebook.imagepipeline.core.ImagePipeline;
import com.facebook.imagepipeline.core.ImagePipelineFactory;
import com.facebook.imagepipeline.request.ImageRequest;
import java.io.File;
import java.io.IOException;
/**
* 以下四个方法可能是你想用的
*
* @see #setToolbarBackGround(int)
* @see #setSaveImageLocalPath(String)
* @see #getSDCacheSizeFormat()
* @see #clearCaches()
* Created by Awen <Awentljs@gmail.com>
*/
public final class FrescoImageLoader extends Awen {
private FrescoImageLoader() {
}
public static void evictFromMemoryCache(String url){
if (url != null && !TextUtils.isEmpty(url)) {
ImagePipeline imagePipeline = Fresco.getImagePipeline();
Uri uri = Uri.parse(url);
if (imagePipeline.isInBitmapMemoryCache(uri)) {
imagePipeline.evictFromMemoryCache(uri);
}
}
}
/**
* 图片是否已经存在了
*/
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();
}
/**
* 本地缓存文件
*/
public static File getCache(Context context, Uri uri) {
if (!isCached(context, uri))
return null;
ImageRequest imageRequest = ImageRequest.fromUri(uri);
CacheKey cacheKey = DefaultCacheKeyFactory.getInstance().getEncodedCacheKey(imageRequest, context);
BinaryResource resource = ImagePipelineFactory.getInstance().getMainFileCache().getResource(cacheKey);
return ((FileBinaryResource) resource).getFile();
}
public static File getCache(ImageRequest request) {
CacheKey cacheKey = DefaultCacheKeyFactory.getInstance().getEncodedCacheKey(request, null);
BinaryResource bRes = ImagePipelineFactory.getInstance().getMainFileCache().getResource(cacheKey);
return ((FileBinaryResource) bRes).getFile();
}
public static byte[] getByte(Context context, Uri uri) throws IOException {
if (!isCached(context, uri))
return null;
ImageRequest imageRequest = ImageRequest.fromUri(uri);
CacheKey cacheKey = DefaultCacheKeyFactory.getInstance().getEncodedCacheKey(imageRequest, null);
BinaryResource bRes = ImagePipelineFactory.getInstance().getMainFileCache().getResource(cacheKey);
return bRes.read();
}
public static byte[] getByte(ImageRequest request) throws IOException {
CacheKey cacheKey = DefaultCacheKeyFactory.getInstance().getEncodedCacheKey(request, null);
BinaryResource bRes = ImagePipelineFactory.getInstance().getMainFileCache().getResource(cacheKey);
return bRes.read();
}
/**
* 清空所有内存缓存
*/
public static void clearMemoryCaches() {
Fresco.getImagePipeline().clearMemoryCaches();
}
/**
* 清空所有磁盘缓存
*/
public static void clearDiskCaches() {
Fresco.getImagePipeline().clearDiskCaches();
}
/**
* 清除所有缓存(包括内存+磁盘)
*/
public static void clearCaches() {
clearMemoryCaches();
clearDiskCaches();
}
/**
* 获取磁盘上主缓存文件缓存的大小
*/
public static long getMainFileCache() {
Fresco.getImagePipelineFactory().getMainFileCache().trimToMinimum();
return Fresco.getImagePipelineFactory().getMainFileCache().getSize();
}
/**
* 获取磁盘上副缓存(小文件)文件缓存的大小
*/
public static long getSmallImageFileCache() {
Fresco.getImagePipelineFactory().getSmallImageFileCache().trimToMinimum();
return Fresco.getImagePipelineFactory().getSmallImageFileCache().getSize();
}
/**
* 获取磁盘上缓存文件的大小
*/
public static long getSDCacheSize() {
return getMainFileCache() + getSmallImageFileCache();
}
/**
* 获取磁盘上缓存文件的大小,带单位,比如:20MB
*/
public static String getSDCacheSizeFormat() {
return FileSizeUtil.formatFileSize(getSDCacheSize());
}
public static String getFileUrl(String url) {
return UriUtil.LOCAL_FILE_SCHEME + ":///" + url;
}
public static String getResUrl(int resId) {
return UriUtil.LOCAL_RESOURCE_SCHEME + ":///" + resId;
}
public static String getAssetUrl(String assetId) {
return UriUtil.LOCAL_ASSET_SCHEME + ":///" + assetId;
}
/**
* 启动一个视频播放
* @param activity
* @param videoUrl 本地视频地址或网络视频地址
*/
public static void startVideoActivity(Activity activity, String videoUrl){
activity.startActivity(new Intent(activity, VideoPlayActivity.class)
.putExtra("videoUrl", videoUrl));
activity.overridePendingTransition(R.anim.image_pager_enter_animation, 0);
}
}