无法传递结果-无法执行非无效方法openCamera [英] Failure delivering result - Cannot execute non-void method openCamera
问题描述
我正在使用Google的 EasyPermissions 库来处理Android 6上的权限.捕获图像的按钮,它要求我授予CAMERA
和WRITE_EXTERNAL_STORAGE
的权限.
I'm using EasyPermissions library from Google to handle Permissions on Android 6. The first time I click the button to capture image it asks me to give permissions for CAMERA
and for WRITE_EXTERNAL_STORAGE
.
在我接受了两个权限的应用程序崩溃后,出现错误消息,您可以在下面看到:
After I have accepted both permissions app crashes with the error message you can see below :
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.debug, PID: 22768
java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=123, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.example.debug/com.example.camera.CameraActivity}: java.lang.RuntimeException: Cannot execute non-void method openCamera
at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.RuntimeException: Cannot execute non-void method openCamera
at pub.devrel.easypermissions.EasyPermissions.runAnnotatedMethods(EasyPermissions.java:229)
at pub.devrel.easypermissions.EasyPermissions.onRequestPermissionsResult(EasyPermissions.java:186)
at com.example.camera.CameraActivity.onRequestPermissionsResult(CameraActivity.java:243)
at android.app.Activity.dispatchRequestPermissionsResult(Activity.java:6553)
at android.app.Activity.dispatchActivityResult(Activity.java:6432)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
和openCamera()
方法:
@Override
@AfterPermissionGranted(RC_CAMERA_PERM)
public void openCamera(int option) {
if (EasyPermissions.hasPermissions(this, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Have permission, do the thing!
Log.i(TAG, "openCamera Has Permissions ");
Intent intent;
switch (option) {
case RECORD_VIDEO :
intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, FIVE_MINS_IN_SECS);
startActivityForResult(intent, RECORD_VIDEO);
break;
case CAPTURE_IMAGE :
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "Image File name");
mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(intent, CAPTURE_IMAGE);
break;
default:
Log.i(TAG, "openCamera wrong option ");
break;
}
} else {
// Ask for Camera permission
EasyPermissions.requestPermissions(this, getString(R.string.ask_camera_permission),
RC_CAMERA_PERM, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
}
推荐答案
您会收到该异常,因为EasyPermission无法调用方法openCamera
,因为它具有参数option
.用AfterPermissionGranted
注释的方法必须为空,以便EasyPermission可以调用它们.
You get that exception because EasyPermission can't invoke your method openCamera
since it has the parameter option
. The methods annotated with AfterPermissionGranted
must be void so EasyPermission can invoke them.