欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 异步获取网络图片并处理导致内存溢出问题解决方法

程序员文章站 2023-12-12 20:07:04
测试环境为adnroid 2.1以上。 1.androidmanifest.xml 权限配置: 添加互联网访问权限: 复制代码 代码如下:
测试环境为adnroid 2.1以上。
1.androidmanifest.xml 权限配置
添加互联网访问权限:
复制代码 代码如下:

<uses-permission android:name="android.permission.internet" />

2.异步图片类 imagedownloadtask
复制代码 代码如下:

import java.io.bytearrayoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.net.malformedurlexception;
import java.net.url;
import android.graphics.bitmap;
import android.graphics.bitmapfactory;
import android.os.asynctask;
import android.widget.imageview;
public class imagedownloadtask extends asynctask<object, object, bitmap> {
private imageview imageview = null;
/***
* 这里获取到手机的分辨率大小
* */
public void setdisplaywidth(int width) {
_displaywidth = width;
}
public int getdisplaywidth() {
return _displaywidth;
}
public void setdisplayheight(int height) {
_displayheight = height;
}
public int getdisplayheight() {
return _displayheight;
}
public int getdisplaypixels() {
return _displaypixels;
}
private int _displaywidth = 480;
private int _displayheight = 800;
private int _displaypixels = _displaywidth * _displayheight;
@override
protected bitmap doinbackground(object... params) {
// todo auto-generated method stub
bitmap bmp = null;
imageview = (imageview) params[1];
try {
string url = (string) params[0];
bmp = getbitmap(url, _displaypixels,true);
} catch (exception e) {
return null;
}
return bmp;
}
protected void onpostexecute(bitmap result) {
if (imageview != null&&result!=null) {
imageview.setimagebitmap(result);
if (null != result && result.isrecycled() == false)
system.gc();
}
}
/**
* 通过url获得网上图片。如:http://www.xxxxxx.com/xx.jpg
* */
public bitmap getbitmap(string url, int displaypixels, boolean isbig) throws malformedurlexception, ioexception {
bitmap bmp = null;
bitmapfactory.options opts = new bitmapfactory.options();
inputstream stream = new url(url).openstream();
byte[] bytes = getbytes(stream);
//这3句是处理图片溢出的begin( 如果不需要处理溢出直接 opts.insamplesize=1;)
opts.injustdecodebounds = true;
bitmapfactory.decodebytearray(bytes, 0, bytes.length, opts);
opts.insamplesize = computesamplesize(opts, -1, displaypixels);
//end
opts.injustdecodebounds = false;
bmp = bitmapfactory.decodebytearray(bytes, 0, bytes.length, opts);
return bmp;
}
/**
* 数据流转成btyle[]数组
* */
private byte[] getbytes(inputstream is) {
bytearrayoutputstream baos = new bytearrayoutputstream();
byte[] b = new byte[2048];
int len = 0;
try {
while ((len = is.read(b, 0, 2048)) != -1) {
baos.write(b, 0, len);
baos.flush();
}
} catch (ioexception e) {
e.printstacktrace();
}
byte[] bytes = baos.tobytearray();
return bytes;
}
/****
* 处理图片bitmap size exceeds vm budget (out of memory 内存溢出)
*/
private int computesamplesize(bitmapfactory.options options,
int minsidelength, int maxnumofpixels) {
int initialsize = computeinitialsamplesize(options, minsidelength,
maxnumofpixels);
int roundedsize;
if (initialsize <= 8) {
roundedsize = 1;
while (roundedsize < initialsize) {
roundedsize <<= 1;
}
} else {
roundedsize = (initialsize + 7) / 8 * 8;
}
return roundedsize;
}
private int computeinitialsamplesize(bitmapfactory.options options,
int minsidelength, int maxnumofpixels) {
double w = options.outwidth;
double h = options.outheight;
int lowerbound = (maxnumofpixels == -1) ? 1 : (int) math.ceil(math
.sqrt(w * h / maxnumofpixels));
int upperbound = (minsidelength == -1) ? 128 : (int) math.min(
math.floor(w / minsidelength), math.floor(h / minsidelength));
if (upperbound < lowerbound) {
return lowerbound;
}
if ((maxnumofpixels == -1) && (minsidelength == -1)) {
return 1;
} else if (minsidelength == -1) {
return lowerbound;
} else {
return upperbound;
}
}
}

3.测试调用代码
复制代码 代码如下:

@override
public void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.main);
imagedownloadtask imgtask =new imagedownloadtask();
/**这里是获取手机屏幕的分辨率用来处理 图片 溢出问题的。begin*/
displaymetrics dm = new displaymetrics();
getwindowmanager().getdefaultdisplay().getmetrics(dm);
imgtask.setdisplaywidth(dm.widthpixels);
imgtask.setdisplayheight(dm.heightpixels);
//end
imageview imageview_test= (imageview)findviewbyid(r.id.imageview_test);
imgtask.execute("http://pic.qukantu.com/big/7515/201201031116491.jpg",imageview_test);
}

4.小结
主要是通过 extends asynctask<object, object, bitmap> 来实现异步的。
图片out of memory 内存溢出 这一块操作,在实际应用中应该考虑淡定抽取出来。这里为了方便放进来了。 溢出处理实际上就是获得设备分辨率把图片进行压缩。

上一篇:

下一篇: