2014-10-18 23 views
0

下載圖像,我用這個方法來下載圖像如何實現此方法,從URL中的AsyncTask

public static Bitmap getBitmapFromURL(String u, int width, int height) { 
     try { 

      URL url = new URL(u); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      connection.setDoInput(true); 
      connection.connect(); 
      InputStream input = connection.getInputStream(); 

      // First decode with inJustDecodeBounds=true to check dimensions 
      final BitmapFactory.Options options = new BitmapFactory.Options(); 
      options.inJustDecodeBounds = true; 
      BitmapFactory.decodeStream(input, null, options); 

      // Calculate inSampleSize 
      options.inSampleSize = calculateInSampleSize(options, width, height); 

      // Decode bitmap with inSampleSize set 
      options.inJustDecodeBounds = false; 
      return BitmapFactory.decodeStream(input, null, options); 

     } catch (IOException e) { 
      // Log exception 
      return null; 
     }  
    } 

這種方法和位圖適配器用於顯示每個列表項的圖像:

Bitmap bitmap = PickanteHelper.getBitmapFromURL(image_url, 150, 150); 
holder.itemImage.setImageBitmap(bitmap); 

爲了避免NetworkOnMainThread異常,我想我應該在加載每個圖像時使用AsyncTask?

+0

好,你可以使用支架使用線程和交流回到UI。 itemImage作爲視圖檢查我之前回答的答案關於這些問題.. – Elltz 2014-10-18 12:25:37

+0

不知道它是什麼,我想它像畢加索一樣是自由的?問題是我需要重新計算圖像的inSampleSize,然後才能將它設置在列表項中,以便列表滾動不會滯後 – 2014-10-18 12:26:14

+0

您指的是我嗎? – Elltz 2014-10-18 12:28:37

回答

0

試試這個。

爲什麼不使用通用圖像加載器

// UNIVERSAL IMAGE LOADER SETUP 
    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder() 
      .cacheOnDisc(true).cacheInMemory(true) 
      .imageScaleType(ImageScaleType.EXACTLY) 
      .displayer(new FadeInBitmapDisplayer(300)).build(); 

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
      getApplicationContext()) 
      .defaultDisplayImageOptions(defaultOptions) 
      .memoryCache(new WeakMemoryCache()) 
      .discCacheSize(100 * 1024 * 1024).build(); 

請參考下面的例子

http://javatechig.com/android/universal-image-loader-library-in-android

https://github.com/nostra13/Android-Universal-Image-Loader

0

嘗試是這樣的:

private Bitmap downloadBitmap(String url) { 
// initilize the default HTTP client object 
final DefaultHttpClient client = new DefaultHttpClient(); 

//forming a HttoGet request 
final HttpGet getRequest = new HttpGet(url); 
try { 
    HttpResponse response = client.execute(getRequest); 
    //check 200 OK for success 
    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 { 
      // getting contents from the stream 
      inputStream = entity.getContent(); 
      // decoding stream data back into image Bitmap that android understands 
      final Bitmap bitmap = BitmapFactory.decodeStream(inputStream); 
      return bitmap; 
     } finally { 
      if (inputStream != null) { 
       inputStream.close(); 
      } 
      entity.consumeContent(); 
     } 
    } 
} catch (Exception e) { 
    // You Could provide a more explicit error message for IOException 
    getRequest.abort(); 
    Log.e("ImageDownloader", "Something went wrong while" + 
      " retrieving bitmap from " + url + e.toString()); 
} 
return null; 
}