我不确定如何回收位图。
我有一个应用程序,在列表视图中显示很多项目。问题是,如果我不降低ImageDownloader类中图像的质量,应用程序崩溃。
在下面提供的代码中,如果我更改options.inSampleSize = 4,则图像质量会降低,并且应用程序不会耗尽内存。但是,我想使用高质量的图像,而不会崩溃我的应用程序。
我该怎么做?
我的ImageDownloader的一部分:
static Bitmap downloadBitmap(String url) {
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpClient client = new DefaultHttpClient(params);
final HttpGet getRequest = new HttpGet(url);
try {
HttpResponse response = client.execute(getRequest);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
String[] urlBits = url.split("/");
Bitmap bitmap = MiscHelpers.getBitmapFromAsset(context, "animals/" + urlBits[urlBits.length-2] + ".jpg");
Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url);
return bitmap;
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
inputStream = entity.getContent();
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 1;
final Bitmap bitmap = BitmapFactory.decodeStream(inputStream,null,options);
//final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (Exception e) {
// Could provide a more explicit error message for IOException or IllegalStateException
getRequest.abort();
Log.w("ImageDownloader", "Error while retrieving bitmap from " + url + e.toString());
} finally {
if (client != null) {
//client.close();
}
}
return null;
}
我的ListView适配器:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Inflate the layout
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_single, parent, false);
ImageView image = (ImageView) rowView.findViewById(R.id.picture);
TextView firstline = (TextView) rowView.findViewById(R.id.firstline);
TextView secondline = (TextView) rowView.findViewById(R.id.secondline);
// Get information about the report
AnimalLocationLog current = objects.get(position);
// Get all the important information
String species = current.getSpecies();
String sanitizedSpecies = MiscHelpers.sanitizeString(species);
int reportId = objects.get(position).getTrackerId();
// Construct the URL to the image
String imageLocation = websiteUrl + sanitizedSpecies + separator + reportId + extension;
// Display user report
downloader.download(imageLocation, image);
firstline.setText(species);
secondline.setText("Spotted " + current.getDateTime().toString("yyyy-MM-dd H:m"));
urlToPath = downloader.getUrlToPath();
return rowView;
}
答案 0 :(得分:1)
我没有看到任何明显的错误。也许在图像编辑方面可以做些什么来使图像更小?
我建议采用堆转储并将其加载到Eclipse MAT https://www.eclipse.org/mat/中,以全面了解正在发生的事情。特别是我建议查看Dominators报告。