【问题标题】:react-native-document-picker not returning anythingreact-native-document-picker 不返回任何内容
【发布时间】:2020-01-17 21:10:30
【问题描述】:

不知道这是因为 react native 版本还是其他原因导致了这种情况发生。所以我刚刚将我的 android 应用程序与 React Native 集成并迁移到 AndroidX。在我进行集成和迁移之前,这段代码非常好,我得到了结果和一切。但是,整合和迁移后,我只能从图片列表中查看和点击照片,没有结果,console.log() 不起作用,甚至我在catch 块上也没有任何错误

我正在使用来自 https://github.com/Elyx0/react-native-document-picker 的 react-native-document-picker 和 React Native 0.60.5

我试过使用这个不同的库https://www.npmjs.com/package/react-native-file-picker,但问题是一样的

try {
    const res = await DocumentPicker.pick({
        type: [DocumentPicker.types.images],
    });

    console.log(res) //not showing anything

    if (res.size <= 3145728) {// 3 MB 
        dispatch({
        type: ACTION_TYPE_PICK_PHOTO_PROFILE_SUCCESS,
        payload: res,
    });
    } else {
    dispatch({
        type: ACTION_TYPE_PICK_PHOTO_PROFILE_EXCEED,
        payload: res
    });
    }
} catch (err) {
    console.log(err) //not showing anything

    if (DocumentPicker.isCancel(err)) {
    // User cancelled the picker, exit any dialogs or menus and move on
    }
}

任何帮助都会很棒!提前致谢。

【问题讨论】:

    标签: javascript android react-native androidx


    【解决方案1】:

    非常感谢@arthur-bachtiar 提供的解决方案,我的情况就是这样:

    @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
    ...
    }
    

    我真的很失望,直到今天,图书馆的维护者还没有在存储库上澄清 this existing issue,也没有包含任何工作示例或有关此过程的安装步骤的说明。

    【讨论】:

      【解决方案2】:

      解决了。原来我忘了为 onActivityResult 添加函数。这是我使用的代码。

          private final int OVERLAY_PERMISSION_REQ_CODE = 1;
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
      
              SoLoader.init(this,false);
              mReactRootView = new RNGestureHandlerEnabledRootView(this);
              mReactInstanceManager = ReactInstanceManager.builder()
                      .setApplication(getApplication())
                      .setCurrentActivity(this)
                      .setBundleAssetName("index.android.bundle")
                      .setJSMainModulePath("index")
                      .addPackage(new MainReactPackage())
                      .setUseDeveloperSupport(BuildConfig.DEBUG)
                      .setInitialLifecycleState(LifecycleState.RESUMED)
                      .build();
      
              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
                  Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                          Uri.parse("package:" + getPackageName()));
                  startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
              } else {
                  startReactNative();
              }
          }
      
          private void startReactNative() {
              // The string here (e.g. "MyReactNativeApp") has to match
              // the string in AppRegistry.registerComponent() in index.js
              mReactRootView.startReactApplication(mReactInstanceManager, "NusaTalent", null);
      
              setContentView(mReactRootView);
          }
      
          @Override
          protected void onActivityResult(int requestCode, int resultCode, Intent data) {
              super.onActivityResult(requestCode, resultCode, data);
      
              if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
                  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                      if (!Settings.canDrawOverlays(this)) {
                          Log.e(TAG, "onActivityResult: SYSTEM_ALERT_WINDOW permission not granted");
                      } else {
                          startReactNative();
                      }
                  }
              }
              Log.d(TAG, "onActivityResult: " + data);
              mReactInstanceManager.onActivityResult( this, requestCode, resultCode, data );
          }
      

      【讨论】:

        猜你喜欢
        • 2015-12-25
        • 1970-01-01
        • 2020-12-10
        • 2018-07-09
        • 2020-09-14
        • 2021-04-18
        • 2018-10-06
        • 2020-08-02
        • 2021-09-03
        相关资源
        最近更新 更多