2011-04-04 54 views
1

顯示位圖我是新手,Android和過去幾天我無法找到問題的原因:如何下載和使用的AsyncTask

我有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

當我點擊中ListView中的一個項目時,它正確地去ActivityB,和ActivityB顯示圖像。當我按ActivityB上的「後退」按鈕備份到ActivityA,然後再次點擊ListView中的一個項目,我來到ActivityB,然後我看到有人通知過程意外關閉。

當我嘗試調試,我注意到這個問題是在該行:

final Bitmap bitmap = BitmapFactory.decodeStream(new FlushedInputStream(inputStream)); 

這在protected Bitmap downloadBitmap(String url)功能。

我讀了Android的一個bug BitmapFactory.decodeStream所以我加了FlushedInputStream來阻止它。

但是,在我看來,這不是問題的原因,因爲它在第一次加載ActivityB時工作,但不是第二次。也許我有內存泄漏? (圖片很大,內存不支持ActivityA。)

如果是這樣,我該如何清理相關的內存?或者是其他問題?

僅供參考:我的圖像格式爲.jpg,我試圖將它們轉換爲.png,但出現同樣的問題。

+0

你的問題太長了。你要求人們爲你調試你的代碼,而且大多數人沒有時間... – 2011-04-04 18:23:15

+1

好吧,我試圖給所有相關的信息..我認爲在代碼中有一些obviuos問題,我沒有看到,因爲我在android和java中是新手。 – Timun 2011-04-05 07:02:46

+0

你應該添加system.gc()來釋放內存 – 2012-07-26 04:40:38

回答

0

你應該使用一個圖像加載庫,如Picasso或爲你做的辛苦工作。