val takePicture = registerForActivityResult(ActivityResultContracts.TakePicture()) { success: Boolean ->
if (success) {
// The image was saved into the given Uri -> do something with it
}
}
val imageUri: Uri = ...
button.setOnClickListener {
takePicture.launch(imageUri)
}
static final int REQUEST_IMAGE_CAPTURE = 1;
private Bitmap mImageBitmap;
private String mCurrentPhotoPath;
private ImageView mImageView;
然后在onClick中触发这个Intent:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Log.i(TAG, "IOException");
}
// Continue only if the File was successfully created
if (photoFile != null) {
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}
}
添加以下支持方法:
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, // prefix
".jpg", // suffix
storageDir // directory
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
String filePath = uriFilePath.getPath(); // Here is path of your captured image, so you can create bitmap from it, etc.
}
}
}
private void onClickCaptureButton(View view) {
Intent takePictureIntent_ = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent_.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile_ = null;
try {
photoFile_ = createImageFile();
} catch (IOException ex) {
}
if(photoFile_!=null){
picturePath=photoFile_.getAbsolutePath();
}
// Continue only if the File was successfully created
if (photoFile_ != null) {
Uri photoURI_ = FileProvider.getUriForFile(this,
"my.app.com.fileprovider", photoFile_);
takePictureIntent_.putExtra(MediaStore.EXTRA_OUTPUT, photoURI_);
startActivityForResult(takePictureIntent_, REQUEST_IMAGE_CAPTURE);
}
}
}
还有三招:
...
private static String picturePath;
private static final int REQUEST_IMAGE_CAPTURE = 2;
...
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp_ = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new
Date());
String imageFileName_ = "JPEG_" + timeStamp_ + "_";
File storageDir_ = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image_ = File.createTempFile(
imageFileName_, /* prefix */
".jpg", /* suffix */
storageDir_ /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
picturePath= image_.getAbsolutePath();
return image_;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK
){
try {
File file_ = new File(picturePath);
Uri uri_ = FileProvider.getUriForFile(this,
"my.app.com.fileprovider", file_);
rasm.setImageURI(uri_);
} catch (/*IO*/Exception e) {
e.printStackTrace();
}
}
}
而且
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString("safar", picturePath);
// Always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}