-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaches.java
More file actions
163 lines (123 loc) · 4.53 KB
/
Copy pathCaches.java
File metadata and controls
163 lines (123 loc) · 4.53 KB
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
package com.example.kongalong.ximalaya_mvp.imageLoad;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.LruCache;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* Created by kongalong on 2016/11/8.
*/
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
public class Caches {
private Context mContext;
private DiskLruCache mDiskLruCache;
private LruCache<String,Bitmap> mLruCache;
//是否创建了disk缓存
public boolean isCreateDiskCache = false;
public Caches(Context context){
mContext = context;
//
initCaches(context);
}
/**
* 初始化缓存
*/
public void initCaches(Context context){
//分配进程内存1/8大小当作缓存
int memCacheSize = (int) (Runtime.getRuntime().maxMemory()/1024/8);
//初始化lruCache
mLruCache = new LruCache<String, Bitmap>(memCacheSize){
@Override
protected int sizeOf(String key, Bitmap value) {
//计算图片大小
return value.getRowBytes()*value.getHeight()/1024;
}
};
//获取disk缓存文件夹
File diskCacheFile = Utils.getDiskCacheFile(context, Constants.DISK_CACHE_FILE_NAME);
//如果disk空间足够就创建disk缓存
if(Utils.getAvailableSpace(diskCacheFile)>Constants.DISK_CACHE_SIZE){
try {
mDiskLruCache = DiskLruCache.open(diskCacheFile,1,1,Constants.DISK_CACHE_SIZE);
isCreateDiskCache = true;
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 下面是四个数据存取方法
*/
public Bitmap loadDataFromMem(String url){
String key = Utils.getkeyFromUrl(url);
return mLruCache.get(key);
}
public void saveDataToMem(String url,Bitmap bitmap){
String key = Utils.getkeyFromUrl(url);
if(mLruCache.get(key)==null){
mLruCache.put(key,bitmap);
}
}
public Bitmap loadDataFromDisk(String url,int width,int height){
if(!isCreateDiskCache){
return null;
}
Bitmap bitmap = null;
String key = Utils.getkeyFromUrl(url);
BufferedInputStream bis = null;
ByteArrayOutputStream baos = null;
try {
DiskLruCache.Snapshot snapshot = mDiskLruCache.get(key);
if(snapshot!=null){
//通过snapshot获取输入流,即可获取byte数组
InputStream is = snapshot.getInputStream(Constants.DISK_CACHE_INDEX);
bis = new BufferedInputStream(is);
baos = new ByteArrayOutputStream();
int len = 0;
byte[] buf = new byte[Constants.BYTE_BUFFER_SIZE];
while((len = bis.read(buf))!=-1){
baos.write(buf,0,buf.length);
}
//压缩图片
bitmap = CompressBitmap.compressFromBytes(baos.toByteArray(), width, height);
if(bitmap!=null){
//保存到内存
saveDataToMem(url,bitmap);
}
}
} catch (IOException e) {
e.printStackTrace();
}finally {
Utils.closeIs(bis);
Utils.closeOs(baos);
}
return bitmap;
}
public void saveDataToDisk(String url, byte[] bytes){
String key = Utils.getkeyFromUrl(url);
BufferedOutputStream bos = null;
try {
OutputStream os = null;
DiskLruCache.Editor edit = mDiskLruCache.edit(key);
if(edit!=null){
os = edit.newOutputStream(Constants.DISK_CACHE_INDEX);
bos = new BufferedOutputStream(os);
bos.write(bytes,0,bytes.length);
//提交
edit.commit();
mDiskLruCache.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
Utils.closeOs(bos);
}
}
}