Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android 調用系統照相機拍照和錄像,android照相機

Android 調用系統照相機拍照和錄像,android照相機

編輯:關於android開發

Android 調用系統照相機拍照和錄像,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);
        
    }

}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved