Skip to content

Commit ebb683b

Browse files
committed
a
1 parent 7b7f922 commit ebb683b

17 files changed

Lines changed: 365 additions & 45 deletions

File tree

‎Android_Zone_Lib/src/Android/Zone/Image/Compress_Sample_Utils.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ public static Bitmap getSampleBitmap(String filePath,Integer targetWidth, Intege
7777
options.inJustDecodeBounds = false;
7878
return BitmapFactory.decodeFile(filePath, options);
7979
}
80+
public static Bitmap getRawBitmap(String filePath){
81+
return BitmapFactory.decodeFile(filePath);
82+
}
8083

8184
/**
8285
*

‎Android_Zone_Lib/src/Android/Zone/Image/DiskLruUtils.java‎

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
package Android.Zone.Image;
22

3+
import java.io.ByteArrayInputStream;
4+
import java.io.ByteArrayOutputStream;
35
import java.io.File;
6+
import java.io.FileInputStream;
7+
import java.io.FileNotFoundException;
48
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.io.OutputStream;
511

612
import android.content.Context;
713
import android.graphics.Bitmap;
814
import android.graphics.BitmapFactory;
915
import android.util.Log;
16+
import Android.Zone.SD.FileUtils_SD;
1017
import Android.Zone.SD.SdSituation;
1118
import Android.Zone.Utils.AppUtils;
1219
import Android.Zone.Utils.MD5Utils;
20+
import Java.Zone.CustomException.OperationFailException;
21+
import Java.Zone.IO.IOUtils;
22+
import Java.Zone.Utils.FileUtils;
1323

1424
public class DiskLruUtils {
1525
private static DiskLruUtils diskLru = new DiskLruUtils();
@@ -23,6 +33,7 @@ public class DiskLruUtils {
2333

2434
private static final String TAG="DiskLruUtils";
2535
private static boolean writeLog=true;
36+
private static MemoryCache cache=new MemoryCache();
2637
public static void log(String str){
2738
if (writeLog) {
2839
Log.d(TAG, str);
@@ -31,38 +42,72 @@ public static void log(String str){
3142

3243
private DiskLruUtils() {
3344
}
34-
public static DiskLruUtils getInstance(){
35-
return diskLru;
36-
}
3745

3846
/**
3947
* 版本号改变 则自动清除
4048
* @param context
4149
* @return
4250
*/
43-
public void openLru(Context context) {
51+
public static DiskLruUtils openLru(Context context) {
4452
try {
4553
/**
4654
* open()方法接收四个参数,第一个参数指定的是数据的缓存地址,
4755
* 第二个参数指定当前应用程序的版本号,
4856
* 第三个参数指定同一个key可以对应多少个缓存文件,基本都是传1,第四个参数指定最多可以缓存多少字节的数据。
4957
*/
50-
File cacheDir = SdSituation.getDiskCacheDir(context, DirName);
58+
// File cacheDir = SdSituation.getDiskCacheDir(context, DirName);
59+
File cacheDir = FileUtils_SD.FolderCreateOrGet("Love",DirName);
5160
if (!cacheDir.exists()) {
5261
cacheDir.mkdirs();
5362
}
5463
mDiskLruCache = DiskLruCache.open(cacheDir,AppUtils.getAppVersion(context), 1, CacheMax);
5564
} catch (IOException e) {
5665
e.printStackTrace();
5766
}
67+
return diskLru;
5868
}
5969

6070
/**
6171
* 所以只有成功才调用这个方法
6272
*
6373
* @param url
6474
*/
65-
public void addUrl(String url) {
75+
public void addUrl(String url,String path) {
76+
addUrl(url, Compress_Sample_Utils.getRawBitmap(path));
77+
}
78+
private static ByteArrayInputStream bitmapToOs(Bitmap bt){
79+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
80+
bt.compress(Bitmap.CompressFormat.JPEG, 100, baos);// 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
81+
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());// 把压缩后的数据baos存放到ByteArrayInputStream中
82+
return isBm;
83+
}
84+
// -----------------------------------------------------------readToOutStream---------------------------------------------------------------
85+
private static boolean readToOutStream(Bitmap bt,OutputStream os){
86+
InputStream in=bitmapToOs(bt);
87+
byte[] buffer = new byte[1024];
88+
int len = 0;
89+
try {
90+
while ((len = in.read(buffer)) != -1) {
91+
os.write(buffer, 0, len);
92+
}
93+
} catch (IOException e) {
94+
throw new OperationFailException("流读取发生IOException!!!");
95+
} finally {
96+
try {
97+
in.close();
98+
os.close();
99+
} catch (IOException e) {
100+
throw new OperationFailException("流关闭发生异常!");
101+
}
102+
}
103+
return true;
104+
}
105+
/**
106+
* 所以只有成功才调用这个方法
107+
*
108+
* @param url
109+
*/
110+
public void addUrl(String url, Bitmap bm) {
66111
// if (downloadUrlToStream(imageUrl, outputStream)) {
67112
// editor.commit();
68113
// } else {
@@ -71,8 +116,13 @@ public void addUrl(String url) {
71116
String key = MD5Utils.hashKeyForDisk(url);
72117
try {
73118
DiskLruCache.Editor editor = mDiskLruCache.edit(key);
74-
editor.commit();
75-
log("addUrl:"+url);
119+
OutputStream outputStream = editor.newOutputStream(0);
120+
if(readToOutStream(bm, outputStream)){
121+
editor.commit();
122+
log("addUrl:"+url);
123+
}else {
124+
editor.abort();
125+
}
76126
} catch (IOException e) {
77127
e.printStackTrace();
78128
}
@@ -95,11 +145,15 @@ public Bitmap getBitmapByUrl(String url) {
95145
Bitmap bitmap = null;
96146
try {
97147
DiskLruCache.Snapshot snapShot = mDiskLruCache.get(key);
98-
bitmap = BitmapFactory.decodeStream(snapShot.getInputStream(0));
148+
if(snapShot != null){
149+
bitmap = BitmapFactory.decodeStream(snapShot.getInputStream(0));
150+
cache.put(key, bitmap);
151+
log("getBitmapByUrl:"+url);
152+
}
99153
} catch (IOException e) {
100154
e.printStackTrace();
101155
}
102-
return bitmap;
156+
return cache.get(key);
103157
}
104158

105159
/**
@@ -119,7 +173,9 @@ public void flush() {
119173
*/
120174
public void close() {
121175
try {
122-
mDiskLruCache.close();
176+
if (!mDiskLruCache.isClosed()) {
177+
mDiskLruCache.close();
178+
}
123179
} catch (IOException e) {
124180
e.printStackTrace();
125181
}
@@ -129,7 +185,9 @@ public void close() {
129185
*/
130186
public void delete() {
131187
try {
132-
mDiskLruCache.delete();
188+
if (!mDiskLruCache.isClosed()) {
189+
mDiskLruCache.delete();
190+
}
133191
} catch (IOException e) {
134192
e.printStackTrace();
135193
}

‎Android_Zone_Lib/src/Android/Zone/Image/ImageLoaderUtils.java‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
package Android.Zone.Image;
22

3+
import android.graphics.Bitmap;
4+
import android.view.View;
35
import android.widget.ImageView;
6+
47
import com.nostra13.universalimageloader.core.ImageLoader;
8+
import com.nostra13.universalimageloader.core.assist.FailReason;
9+
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
510
public class ImageLoaderUtils {
611
/**
712
* 例子: http的话 任何人都会用就不封装里面了
@@ -24,6 +29,32 @@ public String getStr() {
2429
}
2530
public void displayImage(ImageView imageView,String uri,Type type){
2631
uri=type.getStr()+uri;
27-
ImageLoader.getInstance().displayImage(uri, imageView);
32+
ImageLoader.getInstance().displayImage(uri, imageView,new ImageLoadingListener() {
33+
34+
@Override
35+
public void onLoadingStarted(String imageUri, View view) {
36+
// TODO Auto-generated method stub
37+
38+
}
39+
40+
@Override
41+
public void onLoadingFailed(String imageUri, View view,
42+
FailReason failReason) {
43+
// TODO Auto-generated method stub
44+
45+
}
46+
47+
@Override
48+
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
49+
// TODO Auto-generated method stub
50+
51+
}
52+
53+
@Override
54+
public void onLoadingCancelled(String imageUri, View view) {
55+
// TODO Auto-generated method stub
56+
57+
}
58+
});
2859
}
2960
}

‎Android_Zone_Lib/src/Android/Zone/Image/MemoryCache.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
public class MemoryCache {
1010
// 将HashMap封装成一个线程安全的集合,并且使用软引用的方式防止OOM(内存不足)...
11-
// 由于在ListView中会加载大量的图片.那么为了有效的防止OOM导致程序终止的情况...
11+
// 用于在ListView中会加载大量的图片.那么为了有效的防止OOM导致程序终止的情况...
1212
private Map<String, SoftReference<Bitmap>> cache = Collections.synchronizedMap(new HashMap<String, SoftReference<Bitmap>>());
1313
public Bitmap get(String id) {
1414
if (!cache.containsKey(id))

‎Android_Zone_Test/AndroidManifest.xml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@
9191
android:name="com.example.mylib_test.activity.pop_dialog.Dialog_Pop_Adapter_MainActivity"
9292
android:theme="@android:style/Theme.Light.NoTitleBar" >
9393
</activity>
94+
<activity
95+
android:name="com.example.mylib_test.activity.utils.SoftTestActivity"
96+
android:theme="@android:style/Theme.Light.NoTitleBar" >
97+
</activity>
9498
<activity
9599
android:name="com.example.mylib_test.activity.animal.Animal_MainActivity"
96100
android:theme="@android:style/Theme.Light.NoTitleBar" >
@@ -115,6 +119,10 @@
115119
android:name="com.example.mylib_test.activity.FragmentAdapterCircleTestActivity"
116120
android:theme="@android:style/Theme.Light.NoTitleBar" >
117121
</activity>
122+
<activity
123+
android:name="com.example.mylib_test.activity.three_place.ImageLoaderGridActivity"
124+
android:theme="@android:style/Theme.Light.NoTitleBar" >
125+
</activity>
118126
<activity
119127
android:name="com.example.mylib_test.activity.custom_view.ViewPagerActivity"
120128
android:theme="@android:style/Theme.Light.NoTitleBar" >
165 KB
Loading
870 KB
Loading

‎Android_Zone_Test/res/layout/a_thirdparty.xml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111
android:layout_margin="4dp"
1212
android:onClick="onClick"
1313
android:text="imageLoader" />
14+
15+
<Button
16+
android:id="@+id/imageLoaderGrid"
17+
android:layout_width="wrap_content"
18+
android:layout_height="wrap_content"
19+
android:layout_margin="4dp"
20+
android:onClick="onClick"
21+
android:text="imageLoaderGird" />
1422
<Button
1523
android:id="@+id/sideBar"
1624
android:layout_width="wrap_content"
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="vertical" >
6+
7+
<GridView
8+
android:id="@+id/gridView1"
9+
android:layout_width="match_parent"
10+
android:layout_height="wrap_content"
11+
android:numColumns="3" >
12+
</GridView>
13+
14+
</LinearLayout>

‎Android_Zone_Test/res/layout/a_utils_test.xml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@
4545
android:layout_margin="4dp"
4646
android:text="frammentSwitch测试"
4747
android:onClick="onClick" />
48+
<Button
49+
android:id="@+id/bitmapTest"
50+
android:layout_width="wrap_content"
51+
android:layout_height="wrap_content"
52+
android:layout_margin="4dp"
53+
android:text="位图软缓存工具类测试"
54+
android:onClick="onClick" />
4855

4956
</view.FlowLayout_Zone>
5057

0 commit comments

Comments
 (0)