欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android如何獲取視頻首幀圖片

 更新時(shí)間:2020年04月05日 09:05:33   作者:feng海濤  
這篇文章主要為大家詳細(xì)介紹了Android如何獲取視頻首幀圖片的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

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對(duì)象
  private Button button;//聲明Button對(duì)象

  @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)聽(tīng)器
    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        getFirstframe();
      }
    });
  }

  //獲取視頻首幀圖片并保存到本地
  private void getFirstframe(){
    MediaMetadataRetriever mmr=new MediaMetadataRetriever();//實(shí)例化MediaMetadataRetriever對(duì)象
    String path = Environment.getExternalStorageDirectory() + "/shipin.mp4";
    File file=new File(path);//實(shí)例化File對(duì)象,文件路徑為/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對(duì)象
    if(bitmap!=null){
      Toast.makeText(MainActivity.this, "獲取視頻縮略圖成功", Toast.LENGTH_SHORT).show();
      imageView.setImageBitmap(bitmap);//設(shè)置ImageView顯示的圖片
      //存儲(chǔ)媒體已經(jīng)掛載,并且掛載點(diǎn)可讀/寫(xiě)。
      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對(duì)象
        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>

記得添加文件讀寫(xiě)權(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); 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論