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