Android 如何使用AsyncTask下载和显示位图
我是android新手,最近几天我找不到问题的原因: 我有一个带有列表视图的ActivityA。单击该列表视图中的每个项目都将打开ActivityB, 它将在ImageView中显示从web下载的一些图像。 因此,在ActivityB中,我有一个循环,其中包含以下代码以尝试下载图像:Android 如何使用AsyncTask下载和显示位图,android,memory,download,bitmap,android-asynctask,Android,Memory,Download,Bitmap,Android Asynctask,我是android新手,最近几天我找不到问题的原因: 我有一个带有列表视图的ActivityA。单击该列表视图中的每个项目都将打开ActivityB, 它将在ImageView中显示从web下载的一些图像。 因此,在ActivityB中,我有一个循环,其中包含以下代码以尝试下载图像: ImageView ivPictureSmall = new ImageView(this); ImageDownloader ido = new ImageDownloader(); ido.download(t
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);
类图像下载器
类BitmapDownloaderTask
它位于受保护的位图downloadBitmapString url函数中
我读到Android中有一个关于BitmapFactory.decodeStream的bug,所以我添加了FlushedInputStream来防止它
然而,在我看来,这并不是问题的原因,因为当我第一次加载ActivityB时,它是工作的,但不是第二次。也许我有内存泄漏?图片很大,并且在返回ActivityA后不会重新循环内存
如果是,我如何清理关联的内存?还是问题出在其他方面
仅供参考:我的图像是.jpg格式的,我试图将它们转换为.png,但遇到了相同的问题。你应该使用像或这样的图像加载库来为你做艰苦的工作。你的问题太长了。你要求人们为你调试代码,而大多数人没有时间…好吧,我试着提供所有相关信息。。我认为代码中有一些明显的问题,我没有看到,因为我是android和java的新手。你应该添加system.gc来释放内存
public class ImageDownloader
{
public void download(String url, ImageView imageView)
{
BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
task.execute(url);
}
}
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
final Bitmap bitmap = BitmapFactory.decodeStream(new FlushedInputStream(inputStream));