这是我的代码,下载多个图像时出现内存异常。我已经完成了一个重复的问题,但没有找到合适的答复。
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();
}
答案 0 :(得分:2)
我注意到你没有回收位图。这可能是你问题的根源吗?
答案 1 :(得分: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);
答案 2 :(得分:-1)
使用垃圾收集器..
System.gc();