package com.nenglong.jxpt.client.util;import java.io.File;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.HashMap;import com.nenglong.jxpt.client.util.io.URLConnectionDownloader;import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
public class ImageLoad { private HashMap<String, SoftReference<Bitmap>> imageCache;
private Activity activity; public ImageLoad(Activity activity1) {
imageCache = new HashMap<String, SoftReference<Bitmap>>();
activity = activity1;
} public static String getCachePath(Activity activity) {
File fileDir = activity.getCacheDir();
return fileDir.getParent();
} /**
* 异步下载图片
*
* @param imageUrl
* 图片path
* @param imageCallback
* 回调函数
* @return
*/
public Bitmap loadBitmap(final String imageUrl,int height) {
Bitmap bitmap=null;
if (imageCache.containsKey(imageUrl))
{ SoftReference<Bitmap> softReference = imageCache.get(imageUrl);
bitmap=softReference.get();
//这里为啥imageCache里面有这个地址imageUrl,但是返回的bitmap还是为空??????请问请教高手!
if (bitmap != null) {
return bitmap;
}
} else
{
try {
String cachePath = getCachePath(activity);
String[] arr = imageUrl.split("\\/");
String fileName = Md5.toMd5(imageUrl);
String end = imageUrl.substring(imageUrl.lastIndexOf(".") + 1, imageUrl.length())
.toLowerCase();
File destDir = new File(cachePath + File.separator + "temp");
if (!destDir.exists()) {
destDir.mkdirs();
}
final String savePath = cachePath + File.separator + "temp"
+ File.separator + fileName + "." + end;
File file = new File(savePath);
if (file.exists() == false) {
/* new Thread() {
@Override
public void run() {
try {*/
URLConnectionDownloader.download(imageUrl, savePath);
/*} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start(); */
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeFile(savePath, options); // 此时返回bm为空
options.inJustDecodeBounds = false;
if(height == -1)
{
// 计算缩放比
int be = (int) (options.outHeight / (float) 100);
if (be <= 0)
be = 1;
options.inSampleSize = 1;
}else{
options.inSampleSize = 2;
}
bitmap = BitmapFactory.decodeFile(savePath, options);
imageCache.put(imageUrl, new SoftReference<Bitmap>(bitmap));
if (bitmap != null && bitmap.isRecycled()){
bitmap.recycle();
bitmap = null; // recycle()是个比较漫长的过程,设为null,然后在最后调用System.gc(),效果能好很多
}
} catch (Exception e) {
// TODO: handle exception
Log.e("error", e.getMessage(), e);
//bitmap.recycle();
System.gc();
return null;
}
}
//System.gc();
return bitmap;
} /**
* 下载图片方法
*
* @param url
* 下载路径
* @return Drawable
*/
public static Bitmap loadImageFromUrl(String url) {
URL m;
InputStream i = null;
try {
m = new URL(url);
i = (InputStream) m.getContent();
} catch (Exception e) {
e.printStackTrace();
}
Bitmap d = BitmapFactory.decodeStream(i);
return d;
}}这是个有关处理图片大量加载的工具类,虽然进行了软引用但是还是会报04-18 11:25:57.802: E/AndroidRuntime(545): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
这样的错误,请教路过的高手该这样解决这个问题。给些参考资料代码。
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.net.URL;
import java.util.HashMap;import com.nenglong.jxpt.client.util.io.URLConnectionDownloader;import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
public class ImageLoad { private HashMap<String, SoftReference<Bitmap>> imageCache;
private Activity activity; public ImageLoad(Activity activity1) {
imageCache = new HashMap<String, SoftReference<Bitmap>>();
activity = activity1;
} public static String getCachePath(Activity activity) {
File fileDir = activity.getCacheDir();
return fileDir.getParent();
} /**
* 异步下载图片
*
* @param imageUrl
* 图片path
* @param imageCallback
* 回调函数
* @return
*/
public Bitmap loadBitmap(final String imageUrl,int height) {
Bitmap bitmap=null;
if (imageCache.containsKey(imageUrl))
{ SoftReference<Bitmap> softReference = imageCache.get(imageUrl);
bitmap=softReference.get();
//这里为啥imageCache里面有这个地址imageUrl,但是返回的bitmap还是为空??????请问请教高手!
if (bitmap != null) {
return bitmap;
}
} else
{
try {
String cachePath = getCachePath(activity);
String[] arr = imageUrl.split("\\/");
String fileName = Md5.toMd5(imageUrl);
String end = imageUrl.substring(imageUrl.lastIndexOf(".") + 1, imageUrl.length())
.toLowerCase();
File destDir = new File(cachePath + File.separator + "temp");
if (!destDir.exists()) {
destDir.mkdirs();
}
final String savePath = cachePath + File.separator + "temp"
+ File.separator + fileName + "." + end;
File file = new File(savePath);
if (file.exists() == false) {
/* new Thread() {
@Override
public void run() {
try {*/
URLConnectionDownloader.download(imageUrl, savePath);
/*} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start(); */
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bitmap = BitmapFactory.decodeFile(savePath, options); // 此时返回bm为空
options.inJustDecodeBounds = false;
if(height == -1)
{
// 计算缩放比
int be = (int) (options.outHeight / (float) 100);
if (be <= 0)
be = 1;
options.inSampleSize = 1;
}else{
options.inSampleSize = 2;
}
bitmap = BitmapFactory.decodeFile(savePath, options);
imageCache.put(imageUrl, new SoftReference<Bitmap>(bitmap));
if (bitmap != null && bitmap.isRecycled()){
bitmap.recycle();
bitmap = null; // recycle()是个比较漫长的过程,设为null,然后在最后调用System.gc(),效果能好很多
}
} catch (Exception e) {
// TODO: handle exception
Log.e("error", e.getMessage(), e);
//bitmap.recycle();
System.gc();
return null;
}
}
//System.gc();
return bitmap;
} /**
* 下载图片方法
*
* @param url
* 下载路径
* @return Drawable
*/
public static Bitmap loadImageFromUrl(String url) {
URL m;
InputStream i = null;
try {
m = new URL(url);
i = (InputStream) m.getContent();
} catch (Exception e) {
e.printStackTrace();
}
Bitmap d = BitmapFactory.decodeStream(i);
return d;
}}这是个有关处理图片大量加载的工具类,虽然进行了软引用但是还是会报04-18 11:25:57.802: E/AndroidRuntime(545): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
这样的错误,请教路过的高手该这样解决这个问题。给些参考资料代码。
解决方案 »
- Android 中出现这样的警告 Exported service does not require permission
- jni thread 退出异常 , native thread exited without detaching
- android 上的地图二次开发外包
- 有两个android的设备,这两个设备怎么互相发消息或广播
- 关于手机拍照调用问题
- 求apk的反编译工具和方法
- 匿名类用起来究竟有些什么优点呢?
- Android 内存不足时,退出Activity
- 新建的工程居然编译通不过???
- 当调用系统发短信界面后。。。。。。
- android gstreamer移植
- android文件下载遇到两个问题,请大侠来解决!!
我们一般只存10张左右(还要看大小),其他的还得recycle