zoukankan      html  css  js  c++  java
  • Android中级第十讲--相机录像和查看系统相册图片



                         博客出自:http://blog.csdn.net/liuxian13183,转载注明出处! All Rights Reserved ! 


    录像比较简单,开始录制:

    myCamera.unlock();
    initVideo();
    video_stop.setVisibility(View.VISIBLE);
    // 设置视频文件输出的路径
    String dir = CWFileUtil.getSaveVideoPath();
    File dirFile = new File(dir);
    if (!dirFile.exists()) {
    if (!dirFile.mkdirs()) {
    return;
    }
    }
    File file = new File(dir, System.currentTimeMillis() + ".3gp");
    mediarecorder.setOutputFile(file.getAbsolutePath());
    try {
    // 准备录制
    mediarecorder.prepare();
    // 开始录制
    mediarecorder.start();
    } catch (IllegalStateException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }


    void initVideo() {
    if (mediarecorder == null) {
    mediarecorder = new MediaRecorder();// 创建mediarecorder对象
    // 设置录制视频源为Camera(相机)
    mediarecorder.setCamera(myCamera);
    // 录音源为麦克风
    mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediarecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    // 设置录制完成后视频的封装格式THREE_GPP为3gp.MPEG_4为mp4
    mediarecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    // 设置录制的视频编码h263 h264
    mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    // 视频编码
    // mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);
    // 音频编码
    // mediarecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
    // 预览
    mediarecorder.setPreviewDisplay(surfaceView.getHolder()
    .getSurface());
    // 设置视频录制的分辨率。必须放在设置编码和格式的后面,否则报错
    mediarecorder.setVideoSize(176, 144);
    // 设置录制的视频帧率。必须放在设置编码和格式的后面,否则报错
    mediarecorder.setVideoFrameRate(20);
    mediarecorder.setPreviewDisplay(surfaceView.getHolder()
    .getSurface());
    }
    }

    停止录制:

    if (mediarecorder != null) {
    mediarecorder.stop();// 停止录制
    mediarecorder.release();// 释放资源
    mediarecorder = null;
    }
    myCamera.lock();
    video_stop.setVisibility(View.INVISIBLE);

    查看系统视频:

    Intent videoIntent = new Intent(Intent.ACTION_DEFAULT);
    videoIntent.setDataAndTypeAndNormalize(Uri.fromFile(CWFileUtil.getCurrentVideoPath()),
    "video/3gp");
    context.startActivity(videoIntent);

    注意:路径要具体到一个视频,通过这种方式查到最新录制的视频,然后通过系统相册的返回按钮可以查看其他视频。查看图片也是同样原理,只要把查看类型改为"image/*"即可。

    但有的时候拍完的照片或者视频未显示,可以采用发方播的形式,让系统加载

    正常情况下,用下面方法即可

    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    Uri uri = Uri.fromFile(new File(CWFileUtil.getSavePicPath()));
    intent.setData(uri);
    context.sendBroadcast(intent);

    但特殊情况下,这种不管用,可以用下面的方法,不过弱点的显示缓慢

    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri

    .parse("file://" + CWFileUtil.getScanPicPath())));


  • 相关阅读:
    Spring spEL
    Spring 使用外部部署文件
    Spring 自动装配
    spring 属性配置细节
    hdu 1054 Strategic Game
    fzu 2037 Maximum Value Problem
    将博客搬至CSDN
    HDU 4714 Tree2Cycle
    HDU 1009 The Shortest Path in Nya Graph
    POJ 1942 Paths on a Grid 组合数的优化
  • 原文地址:https://www.cnblogs.com/fengju/p/6174417.html
Copyright © 2011-2022 走看看