Android获取视频首帧图片

Android获取视频首帧图片或第n秒的图片

这里介绍如何获取视频首帧或者第n秒的图片并保存在本地,直接上代码:

import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;//声明ImageView对象
    private Button button;//声明Button对象

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView=(ImageView)findViewById(R.id.imageView);//获取布局管理器中的ImageView控件
        button=(Button)findViewById(R.id.button);//获取布局管理器中的Button控件
        //设置按钮点击事件监听器
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFirstframe();
            }
        });
    }

    //获取视频首帧图片并保存到本地
    private void getFirstframe(){
        MediaMetadataRetriever mmr=new MediaMetadataRetriever();//实例化MediaMetadataRetriever对象
        String path = Environment.getExternalStorageDirectory() + "/shipin.mp4";
        File file=new File(path);//实例化File对象,文件路径为/storage/emulated/0/shipin.mp4 (手机根目录)
        if(!file.exists()){
            Toast.makeText(MainActivity.this, "文件不存在", Toast.LENGTH_SHORT).show();
        }
        mmr.setDataSource(path);
        Bitmap bitmap = mmr.getFrameAtTime(0);  //0表示首帧图片
        mmr.release(); //释放MediaMetadataRetriever对象
        if(bitmap!=null){
            Toast.makeText(MainActivity.this, "获取视频缩略图成功", Toast.LENGTH_SHORT).show();
            imageView.setImageBitmap(bitmap);//设置ImageView显示的图片
            //存储媒体已经挂载,并且挂载点可读/写。
            if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                bitmap.recycle(); //回收bitmap
                return;
            }
            try {
                Calendar now = new GregorianCalendar();
                SimpleDateFormat simpleDate = new SimpleDateFormat("yyyyMMddHHmmss", Locale.getDefault());
                String picture_Name = simpleDate.format(now.getTime()); //获取当前时间戳作为文件名称,避免同名
                String framePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/framePicture/"; //图片保存文件夹
                File frame_file = new File(framePath);
                if (!frame_file.exists()) { //// 如果路径不存在,就创建路径
                    frame_file.mkdirs();
                }
                File picture_file = new File(framePath,picture_Name + ".jpg"); // 创建路径和文件名的File对象
                FileOutputStream out = new FileOutputStream(picture_file);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
                out.flush();
                out.close();   //注意关闭文件流
                Toast.makeText(MainActivity.this, "保存图片成功!", Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                Toast.makeText(MainActivity.this, "保存图片失败!" + e.getMessage().toString(), Toast.LENGTH_LONG).show();
                e.printStackTrace();
            }
        }else{
            Toast.makeText(MainActivity.this, "获取视频缩略图失败", Toast.LENGTH_SHORT).show();
        }
    }
}

界面xml代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@mipmap/ic_launcher"/>
    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="获取视频缩略图"/>
</LinearLayout>

记得添加文件读写权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

效果图如下:
在这里插入图片描述
如果需要获取第n秒的图片,把getFrameAtTime()方法的数值改成n*1000就可以。如需要获取视频第5秒图片,则把上面代码

Bitmap bitmap = mmr.getFrameAtTime(0);  //0表示首帧图片

修改成

Bitmap bitmap = mmr.getFrameAtTime(5*1000);  
发布了20 篇原创文章 · 获赞 26 · 访问量 9444

猜你喜欢

转载自blog.csdn.net/weixin_42574892/article/details/98852819
今日推荐