It is possible to display a video thumbnail from a

2019-02-04 18:03发布

Both of the below works fine on the emulator (2.3.3), but on a real device (Nexus S with 4.1.2) no image is shown for the thumbnail. I will also try to run it on an Android 4 Emulator. If I set a default android:src for the ImageView, it is not shown anymore then. This makes me think that it is replaced, but the ImageView is empty.

public class MainActivity extends Activity {

    ImageView img;

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

        img = (ImageView)findViewById(R.id.img_thumbnail);
        new MyAsync().execute("http://commonsware.com/misc/test.mp4");
    }

    //This version is still not working, but it's more readable (edited: Selvin).
    public class MyAsync extends AsyncTask<String, Void, Bitmap>{

        @Override
        protected Bitmap doInBackground(String... objectURL) {
            //return ThumbnailUtils.createVideoThumbnail(objectURL[0], Thumbnails.MINI_KIND);
            return ThumbnailUtils.extractThumbnail(ThumbnailUtils.createVideoThumbnail(objectURL[0], Thumbnails.MINI_KIND), 100, 100);
        }

        @Override
        protected void onPostExecute(Bitmap result){
             img.setImageBitmap(result);
        }
    }
}

I know that a similar question has been asked before, Displaying video thumbnails in an Android device from a remote video URL, but I have already tried this and same result.

Why doesn't this work on the device and how make it work?

3条回答
beautiful°
2楼-- · 2019-02-04 18:18

Use FFmpegMediaMetadataRetriever to extract a thumbnail at the desired position: FFmpegMediaMetadataRetriever

查看更多
唯我独甜
3楼-- · 2019-02-04 18:32

This is not possible on Android and Java and as far as I know any other language without downloading the entire video (correct me if I'm wrong). It is with good reason that YouTube and any other large video service provide a simple API for getting a video thumb. So if the server where the videos reside doesn't support this kind of API call your in bad luck.

查看更多
对你真心纯属浪费
4楼-- · 2019-02-04 18:32

Faced the same problem on 2.3 when tried to make thumbnail from file which was located in the cache folder. Setting permission flags solved the problem:

videoFile.setReadable(true, false);
查看更多
登录 后发表回答