iTVLobby项目解决方案(3)

来源:互联网 发布:战地之王鼠标宏编程 编辑:程序博客网 时间:2024/05/05 16:12

在一个手机应用中终究会需要加载一些图片资源,在对于图片资源的加载中参考了很多网络资料。暂定为下面这种解决方案。

AsyncBitmapLoader.java是图片缓冲类,采用静态的方式使项目中只存在这一种处理容器,

当请求url图片资源时: 1、会先访问private HashMap<String, SoftReference<Bitmap>> imageCache ;如果再缓存中有指定图片则会优先加载。

  2、然后会访问指定图片的本地sdcard缓存。

  3、最后才会去指定url下载相关图片资源,并缓冲到本地sdcard中

实际代码如下

public void loadBitmap(final String imageURLX,
final IGameListViewCallBack back, final int gid) {
// 在内存缓存中,则返回Bitmap对象
String imageURL = null;
if (imageURLX.indexOf("http://") == -1)
imageURL = Define.IMAGEURL + imageURLX;
else
imageURL = imageURLX;
Log.e(TAG, "load Detail :" + imageURLX);
CodeUtil.ErrLog("load Detail:" + imageURLX);
if (imageCache.containsKey(imageURL)) {
CodeUtil.ErrLog("load Detail:" + 0);
Log.e(TAG, gid + "load Detail :" + 0);
SoftReference<Bitmap> reference = imageCache.get(imageURL);
final Bitmap bitmap = reference.get();
if (bitmap != null) {
handler.post(new Runnable() {
@Override
public void run() {
Log.e(TAG, "" + gid);
back.onCallBack(bitmap, gid, imageURLX);
}
});
return;
}
}
/**
* 加上一个对本地缓存的查找
*/
final String bitmapName = imageURL
.substring(imageURL.lastIndexOf("/") + 1);
File cacheDir = new File("/mnt/sdcard/test/");
if (!cacheDir.exists())
cacheDir.mkdir();
File[] cacheFiles = cacheDir.listFiles();
int i = 0;
if (cacheFiles != null && cacheFiles.length != 0) {
Log.e(TAG, gid + "load Detail :" + 1);
CodeUtil.ErrLog("load Detail:" + 1);
for (; i < cacheFiles.length; i++) {
if (bitmapName.equals(cacheFiles[i].getName())) {
break;
}
}
if (i < cacheFiles.length) {
final File f = new File("/mnt/sdcard/test/" + bitmapName);
if (f.length() != 0) {
handler.post(new Runnable() {
@Override
public void run() {
Log.e(TAG, gid + "load Detail :" + 3);
CodeUtil.ErrLog("load Detail:" + 3);
try {
Bitmap bitmap = BitmapFactory
.decodeFile("/mnt/sdcard/test/"
+ bitmapName);
Log.e(TAG, "" + gid);
back.onCallBack(bitmap, gid, imageURLX);
} catch (Exception e) {
CodeUtil.ErrLog(e);
}
}
});
return;
}else 
{
f.delete() ;
}
}
}


Log.e(TAG, "/88888888888888888");


final Bitmap d = loadImageFromUrl(imageURL, bitmapName, back);
if (d != null) {
imageCache.put(imageURL, new SoftReference<Bitmap>(d));
}
Log.e(TAG, imageURL + ":" + (d == null));
handler.post(new Runnable() {
@Override
public void run() {
if (mAllowLoad) {
Log.e(TAG, "" + gid);
back.onCallBack(d, gid, imageURLX);
}
}
});


}


public Bitmap loadImageFromUrl(String url, String bitmapName,
final IGameListViewCallBack back) {
Bitmap d = null;
InputStream i = null;
FileOutputStream out = null;
CodeUtil.ErrLog("loadImageFromUrl::" + url);
URL m = null;
DataInputStream in = null;
boolean flag = true;


try {
m = new URL(url);
} catch (MalformedURLException e1) {
CodeUtil.ErrLog("load Detail:" + 1);
e1.printStackTrace();
}
CodeUtil.ErrLog("loadImageFromUrl::" + 21);
try {
i = m.openStream();
d = BitmapFactory.decodeStream(i);
return d;
} catch (Exception e) {
CodeUtil.ErrLog(e);
e.printStackTrace();
flag = false;
handler.post(new Runnable() {
@Override
public void run() {
back.OnError(-1);
}
});


CodeUtil.ErrLog("loadImageFromUrl::" + 3);
return d;
} finally {
if (flag) {
File f = new File("/mnt/sdcard/test/" + bitmapName);
CodeUtil.ErrLog("loadImageFromUrl::" + 4);
try {
out = new FileOutputStream(f);
byte [] b = new byte[1024] ;
int c = 0 ; 
while((c = i.read(b))!=-1)
{
out.write(b,0,c) ;
}
} catch (Exception e1) {
CodeUtil.ErrLog(e1);
e1.printStackTrace();
}


CodeUtil.ErrLog("loadImageFromUrl::" + 5);
try {
if (in != null)
i.close();
} catch (IOException e) {
CodeUtil.ErrLog(e);
e.printStackTrace();
}


CodeUtil.ErrLog("loadImageFromUrl::" + 9);
try {
if (out != null)
out.close();
} catch (IOException e) {
CodeUtil.ErrLog(e);
e.printStackTrace();
}
CodeUtil.ErrLog("ooooki3");
}
}
}

还存在一些问题,会持续解决发现的bug

0 0
原创粉丝点击