Android如何獲取視頻首幀圖片
Android獲取視頻首幀圖片或第n秒的圖片,供大家參考,具體內(nèi)容如下
這里介紹如何獲取視頻首幀或者第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控件
//設(shè)置按鈕點(diǎn)擊事件監(jiān)聽器
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFirstframe();
}
});
}
//獲取視頻首幀圖片并保存到本地
private void getFirstframe(){
MediaMetadataRetriever mmr=new MediaMetadataRetriever();//實(shí)例化MediaMetadataRetriever對象
String path = Environment.getExternalStorageDirectory() + "/shipin.mp4";
File file=new File(path);//實(shí)例化File對象,文件路徑為/storage/emulated/0/shipin.mp4 (手機(jī)根目錄)
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);//設(shè)置ImageView顯示的圖片
//存儲(chǔ)媒體已經(jīng)掛載,并且掛載點(diǎn)可讀/寫。
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()); //獲取當(dāng)前時(shí)間戳作為文件名稱,避免同名
String framePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/framePicture/"; //圖片保存文件夾
File frame_file = new File(framePath);
if (!frame_file.exists()) { //// 如果路徑不存在,就創(chuàng)建路徑
frame_file.mkdirs();
}
File picture_file = new File(framePath,picture_Name + ".jpg"); // 創(chuàng)建路徑和文件名的File對象
FileOutputStream out = new FileOutputStream(picture_file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close(); //注意關(guān)閉文件流
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>
記得添加文件讀寫權(quán)限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
效果圖如下:

如果需要獲取第n秒的圖片,把getFrameAtTime()方法的數(shù)值改成n*1000就可以。如需要獲取視頻第5秒圖片,則把上面代碼
Bitmap bitmap = mmr.getFrameAtTime(0); //0表示首幀圖片
修改成
Bitmap bitmap = mmr.getFrameAtTime(5*1000);
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
MVVMLight項(xiàng)目之綁定在表單驗(yàn)證上的應(yīng)用示例分析
這篇文章主要為大家介紹了MVVMLight項(xiàng)目中綁定在表單驗(yàn)證上的應(yīng)用示例及源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步除夕快樂,新年快樂2022-01-01
Android開發(fā)中應(yīng)用程序分享功能實(shí)例
這篇文章主要介紹了Android開發(fā)中應(yīng)用程序分享功能,結(jié)合實(shí)例形式分析了基于Intent實(shí)現(xiàn)Android程序分享功能的技巧,需要的朋友可以參考下2016-02-02
Android使用 Retrofit 2.X 上傳多文件和多表單示例
本篇文章主要介紹了Android使用 Retrofit 2.X 上傳多文件和多表單示例,具有一定的參考價(jià)值,有興趣的小伙伴一起來了解一下2017-08-08
Android實(shí)現(xiàn)滑塊拼圖驗(yàn)證碼功能
這篇文章主要介紹了Android實(shí)現(xiàn)滑塊拼圖驗(yàn)證碼功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
Android launcher中模擬按home鍵的實(shí)現(xiàn)
這篇文章主要介紹了Android launcher中模擬按home鍵的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-05-05
android重力感應(yīng)開發(fā)之微信搖一搖功能
這篇文章主要為大家詳細(xì)介紹了android重力感應(yīng)開發(fā)之微信搖一搖功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05

