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

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

概述测试环境为Adnroid2.1以上。1.AndroidManifest.xml权限配置:添加互联网访问权限:复制代码代码如下:<uses-permissionandroid:name=\"android.permission.INTERNET\"/>2.异步图片类ImageDownloadTask复制代码代 测试环境为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,bytes.length,opts);
opts.inSampleSize = computeSampleSize(opts,-1,displaypixels);
//end
opts.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeByteArray(bytes,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,2048)) != -1) {
baos.write(b,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 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,Bitmap> 来实现异步的。
图片Out Of Memory 内存溢出 这一块 *** 作,在实际应用中应该考虑淡定抽取出来。这里为了方便放进来了。 溢出处理实际上就是获得设备分辨率把图片进行压缩。 总结

以上是内存溢出为你收集整理的Android 异步获取网络图片并处理导致内存溢出问题解决方法全部内容,希望文章能够帮你解决Android 异步获取网络图片并处理导致内存溢出问题解决方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1142579.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存