复制代码 代码如下:
package cn.testmedia
/**
* Demo描述:
* 利用Media
* 并转换为Bitmap存放于SDCard
*
* 特别注意:
* get
*
*/
public class MainActivity extends Activity {
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
private void init(){
mButton=(Button) findViewById(R.id.button);
mButton.setOnClickListener(new ClickListenerImpl());
}
private class ClickListenerImpl implements OnClickListener{
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
getBitmapsFromVideo();
default:
break;
}
}
}
public void getBitmapsFromVideo() {
String dataPath = Environment.getExternalStorageDirectory()+ "/testVideo.mp4";
Media
retriever.setDataSource(dataPath);
// 取得视频的长度(单位为毫秒)
String time = retriever.extract
// 取得视频的长度(单位为秒)
int seconds = Integer.valueOf(time) / 1000;
// 得到每一秒时刻的bitmap比如第一秒,第二秒
for (int i = 1; i <= seconds; i++) {
Bitmap bitmap = retriever.get
String path = Environment.getExternalStorageDirectory()+ File.separator + i + ".jpg";
FileOutputStream fos = null;
try {
fos = new FileOutputStream(path);
bitmap.compress(CompressFormat.JPEG, 80, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
main.xml如下:
复制代码 代码如下:
<RelativeLayout
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"
>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取视频的帧图片"
android:layout_centerInParent="true"
/>
</RelativeLayout>