我写了一个代码来访问相机为个人资料页面拍照。代码如下:
case R.id.textViewTakePhoto:
try {
Utility.showPopupMenuWindow(mContext, false, mPopupWindow);
int camera = ContextCompat.checkSelfPermission(mContext, android.Manifest.permission.CAMERA);
if (camera != PackageManager.PERMISSION_GRANTED) {
Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
else{
ActivityCompat.requestPermissions((Activity) mContext,
new String[]{android.Manifest.permission.CAMERA},
9);
}
} catch (Exception e) {
Log.e(TAG, "Can't access camera" + e.getMessage());
e.printStackTrace();
}
break;
在我的清单文件中,我添加了以下代码:
<uses-permission android:name="android.permission.CAMERA" />
<!--for camera purposes-->
<uses-feature
android:name="android.hardware.camera.any"
android:required="true" />
<uses-feature
android:name="android.hardware.camera.autofocus"
android:required="false" />
compileSdkVersion
是25.当我尝试运行应用程序时,它会抛出错误:
Can't access cameraPermission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE cmp=com.google.android.GoogleCamera/com.android.camera.activity.CaptureActivity } from ProcessRecord{1d685bf 17561:com.dell.engage/u0a215} (pid=17561, uid=10215) with revoked permission android.permission.CAMERA
如何解决此问题
答案 0 :(得分:1)
您在使用或不授予检查权限时使用了错误的代码。见下面的代码:
if (camera != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) mContext,
new String[]{android.Manifest.permission.CAMERA},9);
}
else{
Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
答案 1 :(得分:0)
试试这段代码:
你的清单中的
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
你的onClick方法Java代码
:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, RESULT_LOAD_CAMERA);
然后实施onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RESULT_LOAD_CAMERA && resultCode == RESULT_OK && null != data) {
Bundle bundle = data.getExtras();
final Bitmap btm = (Bitmap) bundle.get("data");
mImgView.setImageBitmap(btm);
} else {
Toast.makeText(MainActivity.this, "no image selected", Toast.LENGTH_LONG).show();
}
}
答案 2 :(得分:0)
您尚未声明任何权限。尝试这样的事情:
index.js
同时检查您是否在清单文件中给出了CAMERA权限,如下所示:
if(ActivityCompat.checkSelfPermission(YourActivity.this, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}else {
String[] permissionRequest = {Manifest.permission.CAMERA};
requestPermissions(permissionRequest, 8675309);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode == 8675309){
if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
} else {
Toast.makeText(EnterDataView.this, "Cannot take photo without permission.", Toast.LENGTH_LONG).show();
}
}
}
答案 3 :(得分:0)
试试这段代码:
@OnClick(R.id.button_camera)
void onClickCamera() {
mPresenter.openCamera(this);
}
@OnClick(R.id.gallery_button)
void onClickGallery() {
mPresenter.openGallery(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mPresenter.startEditing(requestCode, resultCode);
}
@Override
public void startGallery() {
Intent intent = new Intent(HomeActivity.this, GalleryActivity.class);
startActivity(intent);
}
@Override
public void startCamera(Uri uri) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, REQ_CAMERA);
}
}