Android图片异步加载和本地缓存框架 android异步图片缓存框架 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 UniversalImageLoader 这个还可以。不过你可以自己实现一个,不用那么麻烦。 /** * 根据url获得Bitmap,必须在子线程中使用该方法 * @param url * @return * @throws Exception */ public Bitmap getBitmapByUrl(String url) throws Exception { Bitmap bitmap = ImageService.get().getBitmap(url); if (null != bitmap) {return bitmap;} if ((viewUtil.isWifi()) || (viewUtil.isNet() && !SettingService.get().getSetting(SettingService.WIFI_IMG))) { bitmap = fileUtil.getBitmap(url); if (null != bitmap) { String folder = fileUtil.getContextRoot() + Domain.CACHE_IMAGE;fileUtil.createFolder(folder); String imagePath = folder + UpdaterService.get().getImgName(url); String ext = imagePath.substring(imagePath.lastIndexOf(".")); CompressFormat format = (ImageUtil.JPG.equals(ext) || ImageUtil.JPEG.equals(ext) ? Bitmap.CompressFormat.JPEG : Bitmap.CompressFormat.PNG); fileUtil.saveBitmap(bitmap, imagePath, format); ImageService.get().add(url, imagePath); } } return bitmap; }/** * 根据url获得图片来源 * @param url 完整的可访问的url * @return * @throws Exception */ public Bitmap getBitmap(String url) { try { if (!ViewUtil.get().isNet()) { throw new RuntimeException(AbstractActivity.NET_ERR_MSG); } HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.connect();InputStream input = conn.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(input); input.close(); conn.disconnect(); return ImageUtil.get().reduce(bitmap, Container.winSize[0], Container.winSize[1], true); } catch (Exception e) { Log.e("AbstractActivity", "根据url获得图片来源", e); return null; } }/** * 把Bitmap保存为文件 * @param bitmap 图片源 * @param filePath 文件路径,要完整的 * @param format 保存的格式 * @throws Exception */ public void saveBitmap(Bitmap bitmap, String filePath, CompressFormat format) throws Exception { OutputStream output = new FileOutputStream(filePath); bitmap.compress(format, 100, output); output.flush(); output.close(); } ubuntu 下编译android 2.3 报错stubs-32.h找不到 请问android如何实现模式对话框 ? Android 4.03 webview 不能出多个缓冲区,如何设置缓冲算数 android新手问题送分了 像如下图片怎么画出来 急问:Android 1.6 支持h.264么?还是只是支持部分? android 移植问题,大家讨论下! -- 自定义搜索框 关于手机RingMode问题 在手机上模拟总是意外停止 android横屏开机怎么设置 【求助】怎么获取手机的发热情况?
不过你可以自己实现一个,不用那么麻烦。
* 根据url获得Bitmap,必须在子线程中使用该方法
* @param url
* @return
* @throws Exception
*/
public Bitmap getBitmapByUrl(String url) throws Exception {
Bitmap bitmap = ImageService.get().getBitmap(url);
if (null != bitmap) {return bitmap;}
if ((viewUtil.isWifi()) || (viewUtil.isNet() && !SettingService.get().getSetting(SettingService.WIFI_IMG))) {
bitmap = fileUtil.getBitmap(url);
if (null != bitmap) {
String folder = fileUtil.getContextRoot() + Domain.CACHE_IMAGE;fileUtil.createFolder(folder);
String imagePath = folder + UpdaterService.get().getImgName(url);
String ext = imagePath.substring(imagePath.lastIndexOf("."));
CompressFormat format = (ImageUtil.JPG.equals(ext) || ImageUtil.JPEG.equals(ext) ? Bitmap.CompressFormat.JPEG : Bitmap.CompressFormat.PNG);
fileUtil.saveBitmap(bitmap, imagePath, format);
ImageService.get().add(url, imagePath);
}
}
return bitmap;
}/**
* 根据url获得图片来源
* @param url 完整的可访问的url
* @return
* @throws Exception
*/
public Bitmap getBitmap(String url) {
try {
if (!ViewUtil.get().isNet()) {
throw new RuntimeException(AbstractActivity.NET_ERR_MSG);
}
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.connect();InputStream input = conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
input.close();
conn.disconnect();
return ImageUtil.get().reduce(bitmap, Container.winSize[0], Container.winSize[1], true);
} catch (Exception e) {
Log.e("AbstractActivity", "根据url获得图片来源", e);
return null;
}
}
/**
* 把Bitmap保存为文件
* @param bitmap 图片源
* @param filePath 文件路径,要完整的
* @param format 保存的格式
* @throws Exception
*/
public void saveBitmap(Bitmap bitmap, String filePath, CompressFormat format) throws Exception {
OutputStream output = new FileOutputStream(filePath);
bitmap.compress(format, 100, output);
output.flush();
output.close();
}