最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android如何獲取視頻首幀圖片

 更新時間:2020年04月05日 09:05:33   作者:feng海濤  
這篇文章主要為大家詳細介紹了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控件
    //設置按鈕點擊事件監(jiān)聽器
    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()) { //// 如果路徑不存在,就創(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();  //注意關閉文件流
        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()方法的數(shù)值改成n*1000就可以。如需要獲取視頻第5秒圖片,則把上面代碼

Bitmap bitmap = mmr.getFrameAtTime(0); //0表示首幀圖片

修改成

Bitmap bitmap = mmr.getFrameAtTime(5*1000); 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

微山县| 闵行区| 咸宁市| 房产| 徐闻县| 连州市| 邓州市| 武宁县| 双流县| 中西区| 湘潭县| 融水| 西畴县| 新建县| 乌苏市| 延津县| 若尔盖县| 闵行区| 嵊州市| 大安市| 淮阳县| 资中县| 手游| 连城县| 平邑县| 孟津县| 高碑店市| 晋城| 南皮县| 论坛| 永修县| 新绛县| 镇远县| 临猗县| 万宁市| 靖边县| 洮南市| 游戏| 牡丹江市| 肃宁县| 临海市|