现在的位置: 首页 > 综合 > 正文

图片内存溢出总结

2013年10月03日 ⁄ 综合 ⁄ 共 1002字 ⁄ 字号 评论关闭
1:加载网络图片采用如下方法的时候。

bitmap = BitmapFactory.decodeStream(uc.getInputStream());

解决方法:先讲图像加载到本地

然后 

bmp = loadBitmapFromLocal(fileWholePath, false);

附上源码

String fileName = linkUrl.substring((linkUrl.lastIndexOf("/")+1));
	String fileWholePath = getCachePath(fileName);
	BufferedOutputStream fout = new BufferedOutputStream(
new FileOutputStream(new File(fileWholePath)));
	InputStream in = uc.getInputStream();
	byte[] data = new byte[1024 * 5];
	int len = 0;
	while((len = in.read(data)) != -1){
		fout.write(data, 0, len);
	}
	fout.flush(); //放倒while循环的外面
	fout.close();
	in.close();

public static Bitmap loadBitmapFromLocal(String localPath, boolean isCache) {
		if(localPath == null){
			return null;
		}
		File file = new File(localPath);
		// 如果缓存图片存在,则返回
		if (file.exists()) {
			Log.d(TAG, "【本地】加载..." + localPath);
			if (BitmapManager.containsKey(localPath)) {
				return BitmapManager.get(localPath);
			} else {
				try {
					String path = file.getAbsolutePath();
					Bitmap bmp = BitmapFactory.decodeFile(path);
					if (isCache && bmp != null) {
						BitmapManager.put(localPath, bmp);
					}
					return bmp;
				} catch (OutOfMemoryError err) {
					err.printStackTrace();
				}
			}
		}
		return null;

抱歉!评论已关闭.