这是我的代码,下载多个图像时出现 outofMemory 异常。我经历了一个重复的问题,但没有找到合适的答复。
try {
HttpGet httpRequest = new HttpGet("http://staging.xyz.com/db/demo_img/abc.png");
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufferedHttpEntity = new BufferedHttpEntity(entity);
InputStream is = bufferedHttpEntity.getContent();
//Drawable d = Drawable.createFromStream(is, "");
//or bitmap
//photos = BitmapFactory.decodeStream(is);
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is,null,o);
//The new size we want to scale to
final int REQUIRED_SIZE=70;
//Find the correct scale value. It should be the power of 2.
int scale=1;
while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
scale*=2;
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
Bitmap photos= BitmapFactory.decodeStream(is, null, o2);
} catch (MalformedURLException e) {
System.out.println("photo error1111");
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
System.out.println("photo error1111");
// TODO Auto-generated catch block
e.printStackTrace();
}