Android,从youtube数据api更有效地获取缩略图?

时间:2012-08-29 14:10:38

标签: java android youtube google-api thumbnails

我的程序是这样的:我将数据api的respopnse传递给第二个活动,即ListActivity。当我进行List映射时,我逐个下载了所有缩略图。它工作,但当它下载缩略图时,UI冻结。我尝试使用AsycTask来做它,但它不起作用。

这是我的代码: 第一个活动

      public void Search(View view){
      String searchText;
      searchText = searchTarget.getText().toString();
      searchText= searchText.replaceAll(space,searchOr);
      if(searchText==null||searchText==""){
          Toast.makeText(Login.this, "Please enter at least one keyword", Toast.LENGTH_LONG).show();
          }
      else{
          try {
              //AsyncTask code here
              String SearchLink=searchTextStart+searchText+searchTextMid+page+searchTextEnd;
              new DownloadTask().execute(SearchLink);
              Log.v("Search checking ", SearchLink);
              } catch(Exception ex) {
                  //your timeout code
                  Toast.makeText(this, "No drivers found, network problem!", Toast.LENGTH_LONG).show();
                  }
          this.pd = ProgressDialog.show(this, "Working..", "Searching...", true, false);
          } 
      }

第二个活动

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Bundle bundle = this.getIntent().getExtras(); 
    arrayLength= bundle.getInt("indexToList");
    videoInfo = bundle.getStringArray("videoInfo");

    mData=getData();

    MyAdapter adapter = new MyAdapter(this);
    setListAdapter(adapter);
    }
private List<Map<String, Object>> getData() {

    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

    Log.v("In videoInfo List","length"+arrayLength);       
    Map<String, Object> map;

    for(int j=0, i=0 ; j<arrayLength; j++, i++){
        map = new HashMap<String, Object>();
        map.put("uploaded", videoInfo[j]);
        j++;
        map.put("category", videoInfo[j]);
        j++;
        map.put("title", videoInfo[j]);
        j++;
        map.put("description", videoInfo[j]);
        j++;

        try {
            Log.i("Check bitmap","before");
            //new DownloadTask().execute(videoInfo[j]);
            //VideoList.this.pd = ProgressDialog.show(VideoList.this, "Working..", "Searching...", true, false);
            Log.i("Check bitmap","after"+videoInfo[j]);
            Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(videoInfo[j]).getContent());
            map.put("thumbnail", bitmap);           
            }
        catch (Exception e){
              Toast.makeText(VideoList.this, "Network problem!", Toast.LENGTH_LONG).show();
        }


        //map.put("thumbnail",thumbnail[i]);            
        j++;            
        map.put("youtube_link", videoInfo[j]);   
        j++;
        map.put("rtsp", videoInfo[j]); 
        Log.v("In videoInfo List","rtsp: "+videoInfo[j]);  
        j++;

        float a = Float.parseFloat(videoInfo[j]);
        int b=(int)a;
        int sec = b % 60;
        int min = b/60;         
        String duration;
        if (sec<10)
        { duration = min+" : 0"+sec;}
        else
        {duration = min+" : "+sec;}
        videoInfo[j] = duration;
        map.put("duration", videoInfo[j]);   
        Log.v("In videoInfo List","duration: "+videoInfo[j]);       
        list.add(map);

        }
    Log.v("In videoInfo List","list: "+list.size());       

    return list;
}

对不起我的英语,如果你不明白我写的请求请告诉我。谁能帮助我?

1 个答案:

答案 0 :(得分:0)

问题解决了!在缩略图下载完成之前,我实际上调用了ListView.setAdapter(适配器)。所以我将ListView.setAdapter(适配器)放在onPostExecute()中,它工作得很好