安卓开发中调用照相功能代码实例

2020-03-02 13:11:00  人气 628    安卓技术  
public class MyCameraActivity extends Activity {
/** Called when the activity is first *d. */
@Override
public void on*(Bundle savedInstanceState) {
super.on*(savedInstanceState);
setContentView(R.layout.main);
Button btnCamera = (Button) findViewById(R.id.btnCamera);
btnCamera.setOnclickListener(new OnclickListener() {
@Override
public void onclick(View v) {
// TODO Auto-generated method stub
letCamera();
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:// 拍照
if (resultCode == RESULT_OK) {
Toast.makeText(this, ”拍摄成功”, Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
protected void letCamera() {
// TODO Auto-generated method stub
Intent imageCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String strImgPath = Environment.getExternalStorageDirectory()
.toString() + ”/dlion/”;// 存放照片的文件夹
String fileName = new SimpleDateFormat(”yyyyMMddHHmmss”)
.format(new Date()) + ”.jpg”;// 照片命名
File out = new File(strImgPath);
if (!out.exists()) {
out.mkdirs();
}
out = new File(strImgPath, fileName);
strImgPath = strImgPath + fileName;// 该照片的绝对路径
Uri uri = Uri.fromFile(out);
imageCaptureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
imageCaptureIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(imageCaptureIntent, 1);
}
}

发表回复

   


  通知楼主
暂无回复!

猜你喜欢

刷新 换一批

首页|新帖|发帖| 赞助

果雨下载网 邮箱 : a@9gy.net
@2024 www.9gy.net 粤ICP-13087322