Android 實現(xiàn)調(diào)用系統(tǒng)照相機拍照和錄像的功能
本文實現(xiàn)android系統(tǒng)照相機的調(diào)用來拍照
項目的布局相當(dāng)簡單,只有一個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="調(diào)用系統(tǒng)相機拍照" />
</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>
相關(guān)代碼如下:
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>
*/
// 激活系統(tǒng)的照相機進行拍照
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);
}
}
實現(xiàn)激活錄像功能的相關(guān)代碼也很簡單:
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>
*/
// 激活系統(tǒng)的照相機進行錄像
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, "調(diào)用照相機完畢", 0).show();
super.onActivityResult(requestCode, resultCode, data);
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android編程實現(xiàn)WebView全屏播放的方法(附源碼)
這篇文章主要介紹了Android編程實現(xiàn)WebView全屏播放的方法,結(jié)合實例形式較為詳細的分析了Android實現(xiàn)WebView全屏播放的布局與功能相關(guān)技巧,需要的朋友可以參考下2015-11-11
Kotlin注解實現(xiàn)Parcelable序列化流程詳解
有時我們會在界面跳轉(zhuǎn)的過程中,做對象傳值,這時就需要對該對象做序列化處理了。Android中對對象的序列化處理有兩種方式,這篇文章主要介紹了Kotlin注解實現(xiàn)Parcelable序列化2022-12-12
android 傳感器(OnSensorChanged)使用介紹
當(dāng)傳感器的值發(fā)生變化時,例如磁阻傳感器方向改變時會調(diào)用OnSensorChanged(). 當(dāng)傳感器的精度發(fā)生變化時會調(diào)用OnAccuracyChanged()方法2014-11-11
Android Fragment滑動組件ViewPager的實例詳解
這篇文章主要介紹了Android Fragment滑動組件ViewPager的實例詳解的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android HorizontalScrollView滑動與ViewPager切換案例詳解
這篇文章主要介紹了Android HorizontalScrollView滑動與ViewPager切換案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-08-08

