2012-03-22 128 views
0

我正在收集youtube的gdata API中的數據並希望使用視頻的縮略圖。我已經有了每個縮略圖的網址,但是我無法讓圖像顯示在我的列表視圖中。位圖當前不返回null,但是我無法用我當前的實現(我知道的?)調用imageview.setImageBitmap(bitmap)方法。請幫助,如果你知道更好的方式來做到這一點!由於從列表視圖適配器中加載URL中的圖像

public class ResultListActivity extends ListActivity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    String searchquery = null; 
    Intent starter = getIntent(); 
    Bundle extras = starter.getExtras(); 
    if (extras != null) { 
     searchquery = extras.getString("search"); 
    }   
    final String URL = "http://gdata.youtube.com/feeds/api/videos?max-results=5&alt=json&author=user&q=" + searchquery; 
    final ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>(); 
    final QueryYoutube query;  

    query = new QueryYoutube(); 
    boolean success = query.searchYoutube(URL); 

    if (success == false) { 
     Toast.makeText(this, "Sorry, no matching results found.", Toast.LENGTH_SHORT).show(); 
     Intent intent = new Intent(ResultListActivity.this, YoutubeActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
     startActivity(intent); 
     finish(); 
    } 

    int size = query.getNumberResults(); 
    HashMap<String, Object> row = new HashMap<String, Object>(); 

    for (int i=0; i<size; i++) { 
     Bitmap bitmap = null; 
     Drawable d = null;   
     try {     
      bitmap = BitmapFactory.decodeStream((InputStream)new URL(query.getThumbnail(i)).getContent()); 
      bitmap.setDensity(Bitmap.DENSITY_NONE); 
      d = new BitmapDrawable(bitmap); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     row = new HashMap<String, Object>(); 
     row.put("Thumbnail", d);    
     row.put("Title", query.getTitle(i)); 
     row.put("Description", query.getDescription(i)); 
     data.add(row); 
    } 

    SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.row, 
       new String[] {"Thumbnail","Title","Description"}, 
       new int[] {R.id.thumb, R.id.title, R.id.description}); 
    setListAdapter(adapter); 

    ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 

    lv.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
      Intent lVideoIntent = new Intent(null, Uri.parse("ytv://"+query.getVideoID(position)), 
        ResultListActivity.this, OpenYouTubePlayerActivity.class); 
      startActivity(lVideoIntent);    
     } 
    }); 

} 
} 

回答

1

我建議的方式提供的URI的圖像這樣做是因爲你應該擴展ArrayAdapter類,然後在getView方法中你發起一個AsyncTask t o在UI線程之外的線程中下載縮略圖,然後將位圖設置爲onPostExecute中的imageview。

你應該使用類似HttpUrlConnection的東西來得到InputStream的圖像解碼成Bitmap。如果您不確定Bitmap的尺寸,在實際創建Bitmap對象之前檢查圖像的尺寸非常重要,因爲它會導致OutOfMemoryError因此會事先設置爲BitmapFactory.OptionsinJustDecodeBoundstrue然後獲取圖像的尺寸,然後選擇一個尺寸足夠的inSampleSize並再次解碼以檢索不太大的Bitmap

您還應該擁有某種Bitmap緩存,以便您不必始終從網上檢索。

0

從文檔的SimpleAdapter

ImageView. The expected bind value is a resource id or a string and setViewImage(ImageView, int) or setViewImage(ImageView, String) is invoked. 

你應該爲縮略圖領域,而不是Bitmap小號

Uri uri = Uri.parse(query.getThumbnail(i)).getContent()); 
+0

我得到了的getContent()方法是未定義類型烏里 – Greg 2012-03-23 14:51:38

0

您至少有兩種方法:
1)您可以使用getView方法,緩存和異步加載thubnails實現自己的列表適配器;
2)您必須執行SimpleAdapter.ViewBinder接口。 事情是這樣的:

private static class GrekViewBinder implements SimpleAdapter.ViewBinder 
{ 
    public boolean setViewValue(View view, Object data, String textRepresentation) 
    { 
     if (view.getId() == R.id.thumb) 
     { 
      ((ImageView)view).setImageXXX(... use `data` argument...); 
      return true; 
     } 
     return false; 
    }  
} 

並結合本活頁夾到你的適配器:

adapter.setViewBinder(new GrekViewBinder()); 
相關問題