尝试向 React Native 添加 openFile 方法,无法调用 getCurrentActivity
Attempting to add an openFile method to React Native, can't call getCurrentActivity
在 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:
事实证明,react-native-fs 在其构建过程中有自己的内部 react-native 依赖项。因此,即使整个应用程序都是基于 react native v0.25 构建的,react-native-fs 是基于 react-native v0.12 构建的,它没有我试图使用的 API。这绝对是疯了。
在 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:
事实证明,react-native-fs 在其构建过程中有自己的内部 react-native 依赖项。因此,即使整个应用程序都是基于 react native v0.25 构建的,react-native-fs 是基于 react-native v0.12 构建的,它没有我试图使用的 API。这绝对是疯了。