问题描述
// 这是我写的代码,我在这里使用视图绑定来在视图中显示图像。
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 (将#修改为@)