React Native Android调用本地拍照并没有返回,求解答



  • 这是我的部分代码:

    public class NativeHelper extends ReactContextBaseJavaModule implements ActivityEventListener {
    
        private static final String TAG = "NativeHelper";
        private ProgressDialog mDialog;
        private Context mContext;
        private String mPath;
        private Uri mUri;
        private Callback mCallback;
        private String mUsername;
    
        public NativeHelper(ReactApplicationContext reactContext) {
            super(reactContext);
            reactContext.addActivityEventListener(this);
        }
    
        @ReactMethod
        public void takePhoto(String username, Callback callback) {
            Activity activity = getCurrentActivity();
            mUsername = username;
            mCallback = callback;
            if (activity != null) {
                try {
                    if (FileHelper.isSdCardExist()) {
                        Log.i(TAG, "Try to invoke camera ");
                        mPath = FileHelper.createAvatarPath(username);
                        File file = new File(mPath);
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                        activity.startActivityForResult(intent, Application.REQUEST_CODE_TAKE_PHOTO);
                    } else {
                        Toast.makeText(activity, activity.getString(R.string.sdcard_not_exist_toast),
                                Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    
     @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            final Activity activity = getCurrentActivity();
            if (resultCode == Activity.RESULT_CANCELED) {
                return;
            }
            if (requestCode == Application.REQUEST_CODE_TAKE_PHOTO) {
                if (mPath != null && activity != null) {
                    Log.i(TAG, "Photo path : " + mPath);
                }
           }
    
        }
    
    }
    

    运行后发现并没有走到onActivityResult()方法里去,打印日志如下:
    小米1s上打印:

    03-03 17:58:06.092 4132-4132/io.jchat.android I/Adreno200-EGL: <qeglDrvAPI_eglInitialize:299>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_REL_RB1.04.01.01.06.041_msm8660_surf_JB_REL_RB1.2_Merge_release_AU (Merge)
                                                                   Build Date: 11/05/12 Mon
                                                                   Local Branch: mybranch425650
                                                                   Remote Branch: quic/jb_rel_rb1.2
                                                                   Local Patches: NONE
                                                                   Reconstruct Branch: AU_LINUX_ANDROID_JB_REL_RB1.04.01.01.06.041 +  NOTHING
    03-03 17:58:06.092 4132-4132/io.jchat.android W/Adreno200-EGL: <qeglDrvAPI_eglSetBlobCacheFuncsQCOM:5575>: EGL_BAD_PARAMETER
    03-03 17:58:06.092 4132-4132/io.jchat.android E/libEGL: eglSetBlobCacheFuncsANDROID resulted in an error: 0x300c
    03-03 17:58:06.262 4132-4132/io.jchat.android W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection
    

    模拟器上打印:

    03-03 04:42:20.804 5645-5735/? W/EGL_emulation: eglSurfaceAttrib not implemented
    03-03 04:42:20.805 5645-5735/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe00d0f80, error=EGL_SUCCESS
    

    求高手解答, 另外如果有更好用React Native的方法实现拍照(CameraRoll API好像并没有相关功能),万望不吝赐教~


  • administrators

    记得有个react-native-camera的第三方组件可以直接用的


登录后回复