SoFunction
Updated on 2025-04-07

Android asynchronously obtains network images and processes solutions to the problem of memory overflow


import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class ImageDownloadTask extends AsyncTask<Object, Object, Bitmap> {
private ImageView imageView = null;
/***
* Get the resolution of the phone here
* */
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) {
(result);
if (null != result && () == false)
();
}
}
/**
* Get online pictures through URL. like:/
* */
public Bitmap getBitmap(String url, int displaypixels, Boolean isBig) throws MalformedURLException, IOException {
Bitmap bmp = null;
opts = new ();
InputStream stream = new URL(url).openStream();
byte[] bytes = getBytes(stream);
//These 3 sentences are the beginning of the image overflow (if there is no need to deal with overflow, directly =1;)
= true;
(bytes, 0, , opts);
= computeSampleSize(opts, -1, displaypixels);
//end
= false;
bmp = (bytes, 0, , opts);
return bmp;
}
/**
* Data flow into btyle[] array
* */
private byte[] getBytes(InputStream is) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[2048];
int len = 0;
try {
while ((len = (b, 0, 2048)) != -1) {
(b, 0, len);
();
}
} catch (IOException e) {
();
}
byte[] bytes = ();
return bytes;
}
/****
* Handle image bitmap size exceeds VM budget (Out Of Memory memory overflow)
*/
private int computeSampleSize( 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( options,
int minSideLength, int maxNumOfPixels) {
double w = ;
double h = ;
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) (Math
.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 : (int) (
(w / minSideLength), (h / minSideLength));
if (upperBound < lowerBound) {
return lowerBound;
}
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
}
}