@AfterPermissionGranted(REQUEST_CAMERA_PERMISSION) private void takePicture() { String[] perms = {Manifest.permission.CAMERA}; if (EasyPermissions.hasPermissions(this, perms)) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (intent.resolveActivity(getPackageManager()) != null) { startActivityForResult(intent, REQUEST_IMAGE_CAPTURE); } } else { EasyPermissions.requestPermissions(this, "需要相机权限来拍照", REQUEST_CAMERA_PERMISSION, perms); } }怎么写,写在哪里,如果加上创建文档用来存储照片,怎么写,完整示例
时间: 2023-12-18 07:05:37 浏览: 129
Android-Camera-master.zip_android camera_camera_camera record
这段代码是一个使用 EasyPermissions 库来请求相机权限,并启动相机拍照的方法。如果您需要在拍照后将照片保存到文件中,可以在 `onActivityResult()` 方法中处理照片数据并保存到指定位置。
完整示例如下:
```java
private static final int REQUEST_CAMERA_PERMISSION = 1;
private static final int REQUEST_IMAGE_CAPTURE = 2;
private String currentPhotoPath;
@AfterPermissionGranted(REQUEST_CAMERA_PERMISSION)
private void takePicture() {
String[] perms = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (EasyPermissions.hasPermissions(this, perms)) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.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
ex.printStackTrace();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
}
} else {
EasyPermissions.requestPermissions(this, "需要相机权限来拍照和存储照片", REQUEST_CAMERA_PERMISSION, perms);
}
}
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
currentPhotoPath = image.getAbsolutePath();
return image;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
// Get the dimensions of the View
int targetW = imageView.getWidth();
int targetH = imageView.getHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(currentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(currentPhotoPath, bmOptions);
imageView.setImageBitmap(bitmap);
// Save the bitmap to a file
try {
FileOutputStream out = new FileOutputStream(currentPhotoPath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
这个示例中,我们在 `takePicture()` 方法中请求了相机和存储权限,然后创建了一个用于保存照片的文件,并启动相机拍照。在 `onActivityResult()` 方法中,我们获取了拍摄的照片数据,并将其显示在一个 ImageView 中。然后,我们将照片保存到之前创建的文件中。请注意,在 `createImageFile()` 方法中,我们使用了 FileProvider 来获取照片文件的 Uri,以便在 Android N 及以上版本中能够正常使用。
阅读全文