当前位置: 编程技术>移动开发
Android 异步获取网络图片并处理导致内存溢出问题解决方法
来源: 互联网 发布时间:2014-10-13
本文导语: 测试环境为Adnroid 2.1以上。 1.AndroidManifest.xml 权限配置: 添加互联网访问权限: 代码如下: 2.异步图片类 ImageDownloadTask 代码如下: import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLEx...
测试环境为Adnroid 2.1以上。
1.AndroidManifest.xml 权限配置:
添加互联网访问权限:
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 {
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(/tech-mobile-dev/url/index.html).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
1.AndroidManifest.xml 权限配置:
添加互联网访问权限:
代码如下:
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 {
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(/tech-mobile-dev/url/index.html).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