无法从youtube上显示缩略图?

时间:2014-10-17 17:42:46

标签: android image youtube thumbnails playlist

此应用应显示所选播放列表中每个视频的缩略图,标题和持续时间,但缩略图仍为白色 该应用程序有效,但它不会显示播放列表中视频的缩略图

我该怎么办?你有什么解决方案吗?

这是我的代码

public class MainActivity extends Activity {

ImageView mainThumb;
TextView mainTitle;
TextView mainTime;
LinearLayout videos;
ArrayList<String> links;

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.videos);

    new ParseVideoDataTask().execute();
    mainThumb = (ImageView) findViewById(R.id.mainThumb);
    mainTitle = (TextView) findViewById(R.id.mainTitle);
    mainTime = (TextView) findViewById(R.id.mainTime);
    videos = (LinearLayout) findViewById(R.id.videos);

}

private class ParseVideoDataTask extends AsyncTask<String, String, String> {
    int count = 0;

    @Override
    protected String doInBackground(String... params) {
        URL jsonURL;
        URLConnection jc;
        links = new ArrayList<String>();
        try {
            jsonURL = new URL("http://gdata.youtube.com/feeds/api/playlists/" +
                    "PL-7t9DoIELCRF7F7bZvvBwO1yvHRFsJiu" +  
                "?v=2&alt=jsonc");
            //PL_VGbflD64WSR1MBcpWlNwsY0lRNVuJ3r
             jc = jsonURL.openConnection(); 
             InputStream is = jc.getInputStream(); 
            String jsonTxt = IOUtils.toString(is);
             JSONObject jj = new JSONObject(jsonTxt); 
             JSONObject jdata = jj.getJSONObject("data"); 
             JSONArray aitems = jdata.getJSONArray("items"); 
             for (int i=0;i<aitems.length();i++) {
                 JSONObject item = aitems.getJSONObject(i); 
                 JSONObject video = item.getJSONObject("video"); 
                 String title = video.getString("title");
                 JSONObject player = video.getJSONObject("player");
                 String link = player.getString("default");
                 String length = video.getString("duration");
                 JSONObject thumbnail = video.getJSONObject("thumbnail"); 
                 String thumbnailUrl = thumbnail.getString("hqDefault");
                 String[] deets = new String[4];
                 deets[0] = title;
                 deets[1] = thumbnailUrl;
                 deets[2] = length;
                 links.add(link);
                 publishProgress(deets);
             }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }       
        return null;
    }       

    @Override
    protected void onProgressUpdate(final String... deets) {
        count++;
        if (count == 1) {
            MainActivity.setImageFromUrl(deets[1], mainThumb, MainActivity.this);
            mainTitle.setText(deets[0]);
            mainTime.setText(formatLength(deets[2]));
            mainThumb.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(links.get(1)));
                    startActivity(i);
                }
            });
        } else {
            LayoutInflater layoutInflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View video = layoutInflater.inflate(R.layout.video, null);
            ImageView thumb = (ImageView) video.findViewById(R.id.thumb);
            TextView title = (TextView) video.findViewById(R.id.title);
            TextView time = (TextView) video.findViewById(R.id.time);
            MainActivity.setImageFromUrl(deets[1], thumb, MainActivity.this);
            title.setText(deets[0]);
            time.setText(formatLength(deets[2]));
            video.setPadding(20, 20, 20, 20);
            videos.addView(video);
            video.setId(count-1);
            video.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(links.get(v.getId())));
                    startActivity(i);
                }
            });
        }
    }
}

private CharSequence formatLength(String secs) {
    int secsIn = Integer.parseInt(secs);
    int hours = secsIn / 3600,
            remainder = secsIn % 3600,
            minutes = remainder / 60,
            seconds = remainder % 60;

            return ((minutes < 10 ? "0" : "") + minutes
            + ":" + (seconds< 10 ? "0" : "") + seconds );
}



public static void setImageFromUrl(String string, ImageView mainThumb2,
        MainActivity mainActivity) {

}



@Override
public void onDestroy() {
    super.onDestroy();
    for (int i=0;i<videos.getChildCount();i++) {
        View v = videos.getChildAt(i);
        if (v instanceof ImageView) {
            ImageView iv = (ImageView) v;           
            ((BitmapDrawable)iv.getDrawable()).getBitmap().recycle();   
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

使用Picasso设置图片网址,如下所示:

Picasso.withContext(this).load(thumbnailUrl).into(myImageView); 

要做到这一点,你需要在doInBackground方法中返回JSON对象,或者使用像Retrofit这样的东西(正如我在上面的评论中提到的那样)来获得一个好的POJO并返回它。然后在postExecute中,您可以使用JSON(或POJO)中的缩略图URL并使用上面的Picasso进行设置。 Picasso将负责为您下载和缓存图像并将其加载到图像视图中。