尝试向React Native添加openFile方法,无法调用getCurrentActivity

时间:2016-06-27 18:14:36

标签: java android react-native react-native-fs

在react-native-fs(https://github.com/johanneslumpe/react-native-fs)的一个分支中,我试图添加此代码:

public class RNFSManager extends ReactContextBaseJavaModule {

    public RNFSManager(ReactApplicationContext reactContext) {
      super(reactContext);
    }
    @ReactMethod
    public void openFile(String filepath, Callback callback) {
        try {
          File file = new File(filepath);
          MimeTypeMap myMime = MimeTypeMap.getSingleton();
          Intent newIntent = new Intent(Intent.ACTION_VIEW);
          String mimeType = myMime.getMimeTypeFromExtension(fileExt(filepath).substring(1));
          newIntent.setDataAndType(Uri.fromFile(file), mimeType);
          newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
          Activity currentActivity = getCurrentActivity();
          if (currentActivity != null) {
            currentActivity.startActivity(newIntent);
          } else {
            this.getReactApplicationContext().startActivity(newIntent);
          }
        } catch (Exception ex) {
          ex.printStackTrace();
          callback.invoke(makeErrorPayload(ex));
        }
      }

但是当我构建它时,我收到了这个错误:

.../android/src/main/java/com/rnfs/RNFSManager.java:138: error: cannot find symbol
      Activity currentActivity = getCurrentActivity();
                                     ^
  symbol: method getCurrentActivity()
1 error

我认为我在核心React Native存储库中使用ReactContextBaseJavaModule的方式与此完全相同:

https://github.com/facebook/react-native/blob/235b16d93287061a09c4624e612b5dc4f960ce47/ReactAndroid/src/main/java/com/facebook/react/modules/intent/IntentModule.java

1 个答案:

答案 0 :(得分:3)

事实证明,react-native-fs在其构建过程中有自己的内部react-native依赖。因此,尽管整个应用程序都是基于本机v0.25的反应,但是react-native-fs正在构建自己的本地v0.12,它没有我试图使用的API。这绝对是疯了。