-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 に答える