-1

这是我的代码,下载多个图像时出现 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();
}
4

3 回答 3

2

我注意到您没有回收位图。这可能是你问题的根源吗?

于 2012-11-15T11:31:21.140 回答
0
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        photos.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

只需添加这个它会节省你的记忆

或者干脆试试这个:

BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize=2;                             CameraAndFolderActivity.myBitmap = BitmapFactory.decodeFile(info.aImagePath, options);  
于 2012-11-15T11:32:37.613 回答
-1

使用垃圾收集器..

System.gc();
于 2012-11-15T12:07:40.300 回答