Android 调用系统照相机拍照和录像

 更新时间:2016年09月08日 16:18:09   作者:wuyudong  
本文主要介绍Android 调用系统照相机拍照和录像的资料,这里整理了详细的代码,有需要的小伙伴可以参考下

本文实现android系统照相机的调用来拍照

项目的布局相当简单,只有一个Button:

<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"
  tools:context=".MainActivity" >

  <Button
    android:onClick="click"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="调用系统相机拍照" />

</RelativeLayout>

首先打开packages\apps\Camera文件夹下面的清单文件,找到下面的代码:

<activity android:name="com.android.camera.Camera"
        android:configChanges="orientation|keyboardHidden"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen"
        android:screenOrientation="landscape"
        android:clearTaskOnLaunch="true"
        android:taskAffinity="android.task.camera">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
      <intent-filter>
        <action android:name="android.media.action.IMAGE_CAPTURE" />
        <category android:name="android.intent.category.DEFAULT" />
      </intent-filter>
      <intent-filter>
        <action android:name="android.media.action.STILL_IMAGE_CAMERA" />
        <category android:name="android.intent.category.DEFAULT" />
      </intent-filter>
    </activity>

相关代码如下:

public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }

  public void click(View view) {
    /*
     * <intent-filter> <action
     * android:name="android.media.action.IMAGE_CAPTURE" /> <category
     * android:name="android.intent.category.DEFAULT" /> </intent-filter>
     */
    // 激活系统的照相机进行拍照
    Intent intent = new Intent();
    intent.setAction("android.media.action.IMAGE_CAPTURE");
    intent.addCategory("android.intent.category.DEFAULT");
    
    //保存照片到指定的路径
    File file = new File("/sdcard/image.jpg");
    Uri uri = Uri.fromFile(file);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    
    startActivity(intent);

  }

}

实现激活录像功能的相关代码也很简单:

public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }

  public void click(View view) {
    /*
     * <intent-filter> <action
     * android:name="android.media.action.VIDEO_CAPTURE" /> <category
     * android:name="android.intent.category.DEFAULT" /> </intent-filter>
     */
    // 激活系统的照相机进行录像
    Intent intent = new Intent();
    intent.setAction("android.media.action.VIDEO_CAPTURE");
    intent.addCategory("android.intent.category.DEFAULT");

    // 保存录像到指定的路径
    File file = new File("/sdcard/video.3pg");
    Uri uri = Uri.fromFile(file);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);

    startActivityForResult(intent, 0);
  }
  
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Toast.makeText(this, "调用照相机完毕", 0).show();
    super.onActivityResult(requestCode, resultCode, data);
    
  }

}

 出处:http://www.cnblogs.com/wuyudong/

相关文章

  • 详解Flutter桌面应用如何进行多分辨率适配

    详解Flutter桌面应用如何进行多分辨率适配

    这篇文章主要为大家介绍了Flutter桌面应用如何进行多分辨率适配的方法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • Android 定时器实现图片的变换

    Android 定时器实现图片的变换

    这篇文章主要介绍了Android 定时器实现图片的变换的相关资料,利用到定时器和handler,message的结合实现改功能,需要的朋友可以参考下
    2017-08-08
  • Android个人手机通讯录开发详解

    Android个人手机通讯录开发详解

    在本篇文章里小编给大家分享了关于Android个人手机通讯录开发的步骤和相关源码,有需要的朋友们学习下。
    2019-02-02
  • Android 七种进度条的样式

    Android 七种进度条的样式

    在开发中我们经常要用到进度条显示下载或者加载的进度。系统自带的黄色进度条在UI效果上经常不能满足策划或者美工的要求。这就要我们屌丝程序员自己自定义进度条,下面小编给大家介绍Android 七种进度条的样式,需要的朋友可以参考下
    2015-08-08
  • 使用RecyclerView添加Header和Footer的方法

    使用RecyclerView添加Header和Footer的方法

    RecyclerView虽然作为ListView的替代者有着较好的性能提升,但是ListView的一些常用功能却没有提供,比如我们平时会经常用到的addHeaderView,addFooterView,既然RecyclerView没有提供这个方法,我们应该如何为列表添加头部和底部呢,接下来通过本文给大家介绍
    2016-03-03
  • android获取时间差的方法

    android获取时间差的方法

    这篇文章主要介绍了android获取时间差的方法,涉及Android操作时间的相关技巧,需要的朋友可以参考下
    2015-04-04
  • Android 自定义Button控件实现按钮点击变色

    Android 自定义Button控件实现按钮点击变色

    这篇文章给大家介绍了android 自定义Button控件实现按钮点击变色的代码,本文给大家附有注释,非常不错,代码简单易懂,对android按钮点击变色的实现感兴趣的朋友参考下吧
    2016-11-11
  • Android编程实现仿QQ发表说说,上传照片及弹出框效果【附demo源码下载】

    Android编程实现仿QQ发表说说,上传照片及弹出框效果【附demo源码下载】

    这篇文章主要介绍了Android编程实现仿QQ发表说说,上传照片及弹出框效果,涉及Android动画特效的相关实现技巧,并附带demo源码供读者下载参考,需要的朋友可以参考下
    2017-01-01
  • Android Tiny集成图片压缩框架的使用

    Android Tiny集成图片压缩框架的使用

    本篇文章主要介绍了Android Tiny集成图片压缩框架的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • 基于Python的Android图形解锁程序详解

    基于Python的Android图形解锁程序详解

    这篇文章主要介绍了基于Python的Android图形解锁程序详解,具有一定借鉴价值,需要的朋友可以参考下。
    2017-11-11

最新评论