Android初级教程调用手机拍照与摄像功能

时间:2023-03-08 16:56:49

这个小案例建议在手机上运行。

package com.example.camera;

import java.io.File;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Toast; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void image(View v){
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);//查看谷歌api按照其步骤走
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"haha.jpg")));//设置保存路径
startActivityForResult(intent, 10);//启动系统拍照功能并返回值
} @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==10){
Toast.makeText(this, "拍照成功", 0).show();
}if(requestCode==20){
Toast.makeText(this, "摄像成功", 0).show();
}
} public void video(View v){
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"haha.3gp")));
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);//设置录像的质量
startActivityForResult(intent, 20);
}
}