在 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: