如何解决我正在尝试使用 android 相机,写了这段代码,但运行后我没有得到任何 O/P,即点击按钮后相机没有打开
// 这是我写的代码,我在这里使用视图绑定来在视图中显示图像。
public void getimage(View view) {
checkStoragePermission();
}
//这里我检查是否授予权限
private void checkStoragePermission() {
if (ContextCompat.checkSelfPermission(
MainActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE) !=
PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("Permission required")
.setMessage("Storage Permission is required to save image")
.setPositiveButton("ALLOW",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
dialog.cancel();
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},MY_PERMISSION__REQUEST_WRITE_EXTERNAL_STORAGE);
}
}).setNegativeButton("DENIED",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,int which) {
dialog.cancel();
}
}).show();
} else {
/*requestPermissionLauncher.launch(
Manifest.permission.REQUESTED_PERMISSION);*/
ActivityCompat.requestPermissions(MainActivity.this,MY_PERMISSION__REQUEST_WRITE_EXTERNAL_STORAGE);
}`} else {
Toast.makeText(this,"Permission Already Granted",Toast.LENGTH_SHORT)
.show();
//capture the image when permission is granted
openCameraToCaptureImage();
}
}`
// 如果权限已经被授予,则打开相机
private void openCameraToCaptureImage() {
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) {
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,"com.ankit.totality_corp_assignment",photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoURI);
startActivityForResult(takePictureIntent,REQUEST_TAKE_PHOTO);
}
}
}
//创建图像文件
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;
}
代码处理 onRequestpermissionResiult()
@Override
public void onRequestPermissionsResult(int requestCode,String[] permissions,int[] grantResults) {
switch (requestCode) {
case MY_PERMISSION__REQUEST_WRITE_EXTERNAL_STORAGE:
// If request is cancelled,the result arrays are empty.
if (grantResults.length > 0 &&
grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//permission is granted
openCameraToCaptureImage();
} else {
Toast.makeText(this,"Permission Denied",Toast.LENGTH_SHORT).show();
}
return;
}
// Other 'case' lines to check for other
// permissions this app might request.
}
我错过了什么我实际上没有得到。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。