我是android新手,最近几天我找不到问题的原因:
我有ActivityA
与ListView。单击ListView中的每个项目将打开ActivityB
,这将显示一些图像下载从网络在一个ImageView
。因此,在ActivityB
中,我有一个循环,其中包含以下代码来尝试下载图像:
ImageView ivPictureSmall = new ImageView(this);
ImageDownloader ido = new ImageDownloader();
ido.download(this.getResources().getString(R.string.images_uri) + strPictureSmall, ivPictureSmall);
ivPictureSmall.setPadding(3, 5, 3, 5);
linearLayout.addView(ivPictureSmall);
类ImageDownloader
public class ImageDownloader
{
public void download(String url, ImageView imageView)
{
BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
task.execute(url);
}
}
类BitmapDownloaderTask
class BitmapDownloaderTask extends AsyncTask
{
private String url;
private final WeakReference imageViewReference;
public BitmapDownloaderTask(ImageView imageView)
{
imageViewReference = new WeakReference(imageView);
}
@Override
// Actual download method, run in the task thread
protected Bitmap doInBackground(String... params)
{
// params comes from the execute() call: params[0] is the url.
return downloadBitmap(params[0]);
}
@Override
// Once the image is downloaded, associates it to the imageView
protected void onPostExecute(Bitmap bitmap)
{
if (isCancelled())
{
bitmap = null;
}
if (imageViewReference != null)
{
ImageView imageView = imageViewReference.get();
if (imageView != null) {
imageView.setImageBitmap(bitmap);
}
}
}
protected Bitmap downloadBitmap(String url)
{
final DefaultHttpClient client = new DefaultHttpClient();
final HttpGet getRequest = new HttpGet(url);
try {
HttpResponse response = client.execute(getRequest);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url);
return null;
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
inputStream = entity.getContent();
final Bitmap bitmap = BitmapFactory.decodeStream(new FlushedInputStream(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: " + e.toString());
} finally {
if (client != null) {
//client.close();
}
}
return null;
}
static class FlushedInputStream extends FilterInputStream
{
public FlushedInputStream(InputStream inputStream)
{
super(inputStream);
}
@Override
public long skip(long n) throws IOException
{
long totalBytesSkipped = 0L;
while (totalBytesSkipped
When I clicked an item in the ListView in ActivityA
, It correctly goes to ActivityB
, and ActivityB
shows the images. When I press the "Back" button on ActivityB
to back up to ActivityA
, then click again on an item in the ListView, I come to ActivityB
and then I see am informed that the process closed unexpectedly.
When I attempt to debug, I noticed that the problem is in the linE:
final Bitmap bitmap = BitmapFactory.decodeStream(new FlushedInputStream(inputStream));
在protected Bitmap downloadBitmap(String url)
函数中
我读到关于BitmapFactory.decodeStream
的Android bug,所以我添加了FlushedInputStream
来防止它。
然而,在我看来这不是问题的原因,因为它是工作时,我第一次加载ActivityB
,但不是第二次。也许我有内存泄漏?(图片太大,返回ActivityA
后内存不回收)
如果有,如何清理相关内存?或者问题出在别的地方?
参考:我的图像是在.jpg
格式,我试图将它们转换为。png,但有同样的问题。
您应该使用Picasso
或Volley
之类的图像加载库来为您完成艰苦的工作。