Android 调用系统照相机拍照和录像
程序员文章站
2024-03-05 21:26:13
本文实现android系统照相机的调用来拍照
项目的布局相当简单,只有一个button:
本文实现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); } }
出处: