2012-11-30 48 views
0

我想從URL讀取圖像並在android圖庫部件中顯示它。如何使背景線程在Android,圖庫部件中加載圖像

所以我寫了下面的代碼在onCreate()方法。

 list = GuideDAO.getAllImages(businessId); 


     Gallery g = (Gallery) findViewById(R.id.gallery); 
     g.setSpacing(2); 

     // Set the adapter to our custom adapter (below) 
     if(list.size() > 0) 
     { 
      g.setAdapter(new ImageAdapter(this,list)); 
     } 

這是我ImageAdapter

public class ImageAdapter extends BaseAdapter { 
     List<Images> glist = null; 
     private String url; 
     public ImageAdapter(Context c,List<Images> lst) { 
      mContext = c; 
      glist = lst; 
      int i=0; 
      for (Images id : glist) { 
       url = id.getImageURL(); // Getting URL 
       InputStream inStream = null; 
        if (url.startsWith("http")) { 

         url = url.replace(" ", "%20"); 
         HttpURLConnection conn; 
         try { 
          conn = (HttpURLConnection)new URL(url).openConnection(); 
          conn.setDoInput(true); 
          conn.connect(); 
          inStream = conn.getInputStream(); 
         } catch (MalformedURLException e) { 

          e.printStackTrace(); 
         } catch (IOException e) { 

          e.printStackTrace(); 
         } 

        } else { 
         try { 
          inStream = new FileInputStream(url); 
         } catch (FileNotFoundException e) { 

          e.printStackTrace(); 
         } 
        } 

        BitmapFactory.Options options = new BitmapFactory.Options(); 
        options.inPreferredConfig = Bitmap.Config.RGB_565; 
        options.inPurgeable = true; 
        Bitmap b = BitmapFactory.decodeStream(inStream, null, options); 

        mImageCollection[i]=b; 


       i++; 
      } 
     } 

     public int getCount() { 
      return mImageIds.length; 
     } 

     public Object getItem(int position) { 
      return position; 
     } 

     public long getItemId(int position) { 
      return position; 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 

      ImageView i = new ImageView(mContext); 

      i.setImageBitmap(mImageCollection[position]); 
      i.setScaleType(ImageView.ScaleType.FIT_XY); 
      i.setLayoutParams(new Gallery.LayoutParams(136, 88)); 
      return i; 
     } 

     private Context mContext; 
     private String[] mImageURLs = {}; 
     private Bitmap[] mImageCollection = {}; 


    } 

這扔錯誤,因爲它不是線程。如何更改此代碼,以便在後臺加載URL和圖片?

所以我改變了我的ImageAdapter通過使用SmartImageView,它處理後臺線程和緩存。

public class ImageAdapter extends BaseAdapter { 
     List<ImageGallery> glist = null; 
     private String url; 
     public ImageAdapter(Context c,List<ImageGallery> lst) { 
      mContext = c; 
      glist = lst; 
      int i=0; 
      al = new ArrayList<String>(); 
      for (ImageGallery id : glist) { 

       al.add(id.getImageURL()); 

      } 
     } 

     public int getCount() { 
      return mImageIds.length; 
     } 

     public Object getItem(int position) { 
      return position; 
     } 

     public long getItemId(int position) { 
      return position; 
     } 

     public View getView(int position, View convertView, ViewGroup parent) { 
      Log.d("deepak", "getview gallery"); 
      SmartImageView i = new SmartImageView(mContext); 
      i.setImageUrl(al.get(position)); 
      i.setScaleType(ImageView.ScaleType.FIT_XY); 
      i.setLayoutParams(new Gallery.LayoutParams(136, 88)); 
      return i; 
     } 

     private Context mContext; 
     private String[] mImageURLs = {}; 
     private ArrayList<String> al; 
     private Bitmap[] mImageCollection = {}; 
     private Integer[] mImageIds = {}; 

    } 

但我的getView()現在沒有被調用。

回答

0

您可以使用智能圖像視圖。 SmartImageView是Android標準ImageView的替代品,它可以從URL或用戶的聯繫人地址簿中加載圖像。圖像緩存到內存和磁盤以實現超快速加載。 請參考以下鏈接瞭解更多信息https://github.com/loopj/android-smart-image-view .hope這可能會幫助你完成你的任務

+0

我使用了智能圖像視圖。似乎很好。但我的GetView沒有被解僱。 – iShare

+0

請檢查getCount()方法返回你的list的正確大小。可能是mImageIds.length;返回0. tht可能是getview()沒有被調用的原因之一。我想你必須在getcount()方法中返回glist的大小。 –

+0

是的,你是對的andriod.fryo – iShare

0

我建議編寫一個AsyncImageLoader類並讓它處理從http下載的圖像。這樣,您可以在單獨的線程上緩存和管理所有內容,並在加載完成後讓它將圖像設置爲視圖。如果你想在其他地方下載圖像,你也可以在整個應用程序中使用這個類。

你可以在你的適配器中調用類似mImageLoader.loadImage(myImageView, Url)的東西,一旦加載完成,它就會將其放入。

如果你想了解更多的細節,請告訴我:)

+0

是的,我想知道更多的信息。你有博客嗎? – iShare

+0

這個示例項目並不完美,但它會給你一個開始 https://github.com/DHuckaby/Prime –