【问题标题】:can't show the thumbnail from youtube?无法显示来自 youtube 的缩略图?
【发布时间】:2014-12-13 08:44:03
【问题描述】:

此应用应显示所选播放列表中每个视频的缩略图、标题和时长,但缩略图保持白色 该应用程序可以运行,但它没有在播放列表中显示视频的缩略图

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

这是我的密码

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();   
        }
    }
}

}

【问题讨论】:

  • 我还没有给你答案,但我建议你使用像 Retrofit square.github.io/retrofit 这样的 Http 库,它可能会让你在处理 Http 端点时更轻松。 :)

标签: android image youtube thumbnails playlist


【解决方案1】:

使用Picasso 设置图片url,如下:

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

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

【讨论】:

  • 所以你需要在你的 AsyncTask 中实现 onPostExecute。您将返回 JSON 对象(或 POJO,如果使用 Retrofit/etc),而不是从 doInBackground 返回 null。然后在 onPostExecute 中,您可以通过 Picasso 加载图像。
  • 很抱歉,我不太明白,你能告诉我一行示例代码吗?请
猜你喜欢
  • 2023-01-19
  • 2013-02-21
  • 2015-09-13
  • 2023-03-07
  • 2011-10-16
  • 2011-03-25
  • 1970-01-01
  • 2020-07-07
  • 2011-07-14
相关资源
最近更新 更多