我试图让我的android相机应用程序在新版本的android上工作,并严格遵循谷歌的开发人员课程“在运行时请求权限”,但我仍然在logcat中遇到错误,当我试图打开相机时,应用程序崩溃。我不明白我做错了什么。下面是我的代码中的相关方法:
private void showPictureDialog() {
AlertDialog.Builder pictureDialog = new AlertDialog.Builder(this);
pictureDialog.setTitle("Select Action");
String[] pictureDialogItems = {
"Select photo from gallery",
"Capture photo from camera"};
pictureDialog.setItems(pictureDialogItems,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
choosePhotoFromGallary();
break;
case 1:
checkPermissionsR();
break;
}
}
});
pictureDialog.show();
}
public void checkPermissionsR() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
takePhotoFromCamera();
} else {
if (shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
Toast.makeText(this, "need external storage permission", Toast.LENGTH_SHORT).show();
}
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_PERMISSIONS);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == REQUEST_PERMISSIONS) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
takePhotoFromCamera();
} else
Toast.makeText(this, "permission was not granted to save images", Toast.LENGTH_SHORT).show();
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
另外,我不认为问题在这里,但我也会包括这一节,以防万一。
private void takePhotoFromCamera() {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == this.RESULT_CANCELED) {
return;
}
if (requestCode == GALLERY) {
if (data != null) {
Uri contentURI = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), contentURI);
String path = saveImage(bitmap);
Toast.makeText(MainActivity.this, "Image Saved!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "Failed!", Toast.LENGTH_SHORT).show();
}
}
} else if (requestCode == CAMERA) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
saveImage(thumbnail);
Toast.makeText(MainActivity.this, "Image Saved!", Toast.LENGTH_SHORT).show();
}
RecyclerView.Adapter newImageAdapter = new ImageAdapter(BingoGalleryFolder); //***************//
recycleView.swapAdapter(newImageAdapter,false);
}
public String saveImage(Bitmap myBitmap) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
File wallpaperDirectory = new File(Environment.getExternalStorageDirectory() + IMAGE_DIRECTORY);
// have the object build the directory structure, if needed.
if (!wallpaperDirectory.exists()) {
wallpaperDirectory.mkdirs();
}
try {
File f = new File(wallpaperDirectory, Calendar.getInstance()
.getTimeInMillis() + ".jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
MediaScannerConnection.scanFile(this,
new String[]{f.getPath()},
new String[]{"image/jpeg"}, null);
fo.close();
Log.d("TAG", "File Saved::--->" + f.getAbsolutePath());
return f.getAbsolutePath();
} catch (IOException e1) {
e1.printStackTrace();
}
return "";
}
6条答案
按热度按时间vfh0ocws1#
在您的情况1中,您正在获取有关写入外部存储的权限,您应该获取有关使用相机的权限,因此在您的清单中首先提到了这样的权限。
然后在**checkPermissionsR()**中获取此权限
jbose2ul2#
尝试在启动应用程序时一次请求所有权限。就像你的
MainActivity
首先,在
onCreate
方法中调用:然后尝试调用这些方法:
cwxwcias3#
我希望这将有助于你和它的作品在我的项目罚款。
svdrlsy44#
试试这个
von4xj4u5#
将此行添加到活动
yqlxgs2m6#
尝试向MANIFEST文件添加权限。
并在checkPermissionsR()中获取此权限