我是Android Studio新手。我想通过点击按钮用手机摄像头拍摄照片,然后显示拍摄的照片,照片将自动保存在手机图库中。我找到了一些在线示例,但我拍摄的照片未保存在图库中。我已经使用了下面的代码。我可以帮忙吗?
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.buttonTakePhoto:
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
break;
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
Bitmap image = (Bitmap) data.getExtras().get("data");
ImageView imageview = (ImageView) findViewById(R.id.imageView);
imageview.setImageBitmap(image);
}
}
答案 0 :(得分:1)
要使用相机意图拍摄图像并将其存储在图库中使用此功能,您将获得onActivityResult
private void takeImageFromCamera() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File exportDir = new File(Environment.getExternalStorageDirectory(), "TempFolder");
if (!exportDir.exists()) {
exportDir.mkdirs();
} else {
exportDir.delete();
}
File mTempCameraPhotoFile = new File(exportDir, "/" + UUID.randomUUID().toString().replaceAll("-", "") + ".jpg");
Log.d("TAG", "/" + UUID.randomUUID().toString().replaceAll("-", "") + ".jpg");
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mTempCameraPhotoFile));
startActivityForResult(takePictureIntent, Config.CAMERA_PIC_REQUEST);
}
}