5

Inside a fork of react-native-fs ( https://github.com/johanneslumpe/react-native-fs ), I'm attempting to add this code:

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));
        }
      }

But when I build it, I get this error:

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

I think I'm using the ReactContextBaseJavaModule in exactly the same way as this, in the core React Native repo:

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

freyley
  • 4,145
  • 3
  • 20
  • 25
  • according to the source code: https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContextBaseJavaModule.java this should work! make sure you are using the latest version by checking the gradle.build and perform a clean on the project – Ardavan Kalhori Jul 05 '16 at 17:29

1 Answers1

3

It turns out that react-native-fs has its own internal react-native dependency in its build process. So even though the whole app was building off of react native v0.25, react-native-fs was building itself off of react-native v0.12, which does not have the APIs I was trying to use. This is absolutely nuts.

freyley
  • 4,145
  • 3
  • 20
  • 25
  • How did you verify that react-native-fs was using a different version of react-native? and were you able to resolve this, I am facing a similar issue with react-native-linkedin-login. https://github.com/jodybrewster/react-native-linkedin-login – rubish Oct 06 '16 at 12:40
  • the react-native-fs folder inside node_modules has a package.json which had a specific version for react-native. I changed that to * on a fork and submitted a PR. – freyley Oct 07 '16 at 15:54
  • Link to the PR? I'm having the same issue. – Matteo Mazzarolo Oct 12 '16 at 13:39