简体   繁体   English

如何从R.raw文件夹中获取视频缩略图?

[英]How can I get video thumbnails from R.raw folder?

The code below can not get the video thumbnails of videos which were located in R.raw folder. 下面的代码无法获取位于R.raw文件夹中的视频的视频缩略图。

I want to use a gallery to show the thumbnails of videos. 我想使用图库来显示视频的缩略图。 Videos were put into R.raw folder (actually I'm new to Android, so I don't really know where to put these videos, so I put them in R.raw folder). 视频被放入R.raw文件夹(实际上我是Android的新手,所以我真的不知道将这些视频放在哪里,所以我将它们放在R.raw文件夹中)。 Now I want to get the thumbnails of the videos. 现在我想获得视频的缩略图。 But someone told that thumbnails can only be got from the SD card. 但是有人告诉说缩略图只能从SD卡中获取。 So is it true? 这是真的吗? And what should I do next? 接下来我该怎么办? Thanks. 谢谢。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final Gallery gallery = (Gallery) findViewById(R.id.gallery);
    final String[] videoFileList = new String[] { "R.raw.01", "R.raw.02" };

    BaseAdapter adapter = new BaseAdapter() {

        public int getCount() {
            return videoFileList.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 imageView = new ImageView(Select.this);
            Bitmap bmThumbnail;
            bmThumbnail = ThumbnailUtils.createVideoThumbnail("android.resource://" + getPackageName() + "/"
                    + videoFileList[position], Thumbnails.MINI_KIND);
            System.out.println(videoFileList[position]);
            imageView.setImageBitmap(bmThumbnail);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            TypedArray typedArray = obtainStyledAttributes(R.styleable.Gallery);
            imageView.setBackgroundResource(typedArray.getResourceId(
                    R.styleable.Gallery_android_galleryItemBackground, 0));
            return imageView;
        }
    };
    gallery.setAdapter(adapter);
    gallery.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(Select.this, Play.class);
            intent.putExtra("image", videoFileList[position]);

            startActivity(intent);
            Select.this.finish();
        }

        public void onNothingClick(AdapterView<?> parent) {
        }
    });
}

On Android 5, createVideoThumbnail() always returned null when asking for the thumbnail of a video in resources. 在Android 5上,当在资源中请求视频的缩略图时, createVideoThumbnail()始终返回null Here's what worked for me in the end: 这是最终对我有用的东西:

Uri videoURI = Uri.parse("android.resource://" + getActivity().getPackageName() + "/raw/animation");
MediaMetadataRetriever retriever = new MediaMetadataRetriever();              
retriever.setDataSource(getActivity().getApplicationContext(), videoURI);
Bitmap thumb = retriever.getFrameAtTime(10, MediaMetadataRetriever.OPTION_PREVIOUS_SYNC);
videoView.setBackground(new BitmapDrawable(getResources(), thumb));

Have you looked at, createVideoThumbnail() from ThumbnailUtils Class? 你有没有看过ThumbnailUtils Class中的createVideoThumbnail()

Actually, Your video files are in resource/raw directory so you have to create Thumbnails for it. 实际上,您的视频文件位于resource/raw目录中,因此您必须为其创建缩略图。

If your video files are in External Storage then Android Media Store itself creates for you. 如果您的视频文件位于外部存储中,那么Android Media Store本身就会为您创建。 And you can get it by MediaStore.Video.Thumbnails Class. 你可以通过MediaStore.Video.Thumbnails Class获得它。

Video on res/raw folder of your application isn't managed by Android Media Content Provider, so you cannot obtain their thumbnail from it. 应用程序的res/raw文件夹上的视频不受Android Media Content Provider管理,因此您无法从中获取缩略图。

Alternatively, you can try to save them as normal file in your SD and, then, ask to media provider for thumbnail or create it directly via ThumbnailUtils.createVideoThumbnail class (only Android 2.2 or above) 或者,您可以尝试将它们保存为SD中的普通文件,然后向媒体提供商询问缩略图或直接通过ThumbnailUtils.createVideoThumbnail类(仅限Android 2.2或更高版本)创建它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM