gpt4 book ai didi

android - 传递结果失败 - 无法执行非无效方法 openCamera

转载 作者:行者123 更新时间:2023-11-29 14:36:17 25 4
gpt4 key购买 nike

我正在使用 EasyPermissions来自 Google 的库,用于处理 Android 6 上的权限。我第一次单击按钮捕获图像时,它要求我授予 CAMERAWRITE_EXTERNAL_STORAGE 权限。

在我接受了这两个权限后,应用程序崩溃并出现错误消息,您可以在下面看到:

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 可以调用它们。

此条件在 EasyPermission 的来源中得到明确验证:

// Method must be void so that we can invoke it
if (method.getParameterTypes().length > 0) {
throw new RuntimeException("Cannot execute non-void method " + method.getName());
}

要解决此问题,您必须从方法 openCamera 中删除任何参数。

关于android - 传递结果失败 - 无法执行非无效方法 openCamera,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36885517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com