多媒体所包含得有很多,列如拍照,录像。。。。,要使用这些先加入权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
拍照
在布局文件中添加如自己想要的一个布局
<SurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/sv_main_surface"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="takePhoto"
android:text="拍照"
/>
在主要的activity中加入代码
public class MainActivity extends AppCompatActivity {
private SurfaceView sv_main_surface;
private Camera camera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sv_main_surface = (SurfaceView) findViewById(R.id.sv_main_surface);
sv_main_surface.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
//打开照相机
camera = Camera.open();
//设置参数
Camera.Parameters parameters=camera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG);
parameters.set("jpeg-quality",85);
camera.setParameters(parameters);
//将画面展示到SurfaceView
try {
camera.setPreviewDisplay(sv_main_surface.getHolder());
//开启预览
camera.startPreview();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
//关闭照相机
if(camera!=null){
camera.release();
camera.stopPreview();
camera=null;
}
}
});
}
public void takePhoto(View view){
//DCIM/Camera/
camera.takePicture(null, null, new Camera.PictureCallback() {
@Override
public void onPictureTaken(byte[] bytes, Camera camera) {
//实例化文件输出流
//写
//压缩(图片)
//将字节数组转成Bitmap
Bitmap bitmap=BitmapFactory.decodeByteArray(bytes,0,bytes.length);
try {
FileOutputStream fos=new FileOutputStream("/mnt/sdcard/dcim/camera/G150831_"+System.currentTimeMillis()+".png");
bitmap.compress(Bitmap.CompressFormat.PNG,85,fos);
//重新继续拍照
camera.stopPreview();
camera.startPreview();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
});
}
}
录音功能的实现也没有太大的偏差,在xml中加入布局
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="开始"
android:onClick="start"
android:layout_weight="1"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="停止"
android:onClick="stop"
android:layout_weight="1"
/>
public void start(View view){
mediaRecorder.reset();
//设置参数
//设置音频,视频来源
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//设置输出格式
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
//设置编码格式(视频,音频)
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//设置保存路径
mediaRecorder.setOutputFile("/mnt/sdcard/G150831_"+System.currentTimeMillis()+".mp4");
//画面
mediaRecorder.setPreviewDisplay(sv_media_recorder_surface.getHolder().getSurface());
//准备
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IOException e) {
e.printStackTrace();
}
}
public void stop(View view){
mediaRecorder.stop();
}
}
在类中实例化录制器
mediaRecorder = new MediaRecorder();