在React Native中创建Java Native模块

问题描述 投票:0回答:1

我正在尝试构建一个本机java模块,我想让一个媒体文件(图像)立即在图库中可用,所以我在某处读到可以在Android中使用MediaScannerConnection API。原生 Android API,并在 React Native 中使用它,所以我是如何制作它的,但我引发了一个错误:

错误:类 MediaScannerPackage 中的构造函数 MediaScannerPackage 不能应用于给定类型; module.add(new MediaScannerPackage(reactContext));

这是我的代码:

MediaScannerPackage.java 
package com.myApp;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MediaScannerPackage implements ReactPackage {
    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
        return Collections.emptyList();
    }

    @Override
    public List<NativeModule> createNativeModules(
            ReactApplicationContext reactContext) {
        List<NativeModule> modules = new ArrayList<>();

        modules.add(new MediaScannerPackage(reactContext));

        return modules;
    }
}

MediaScannerModule.java

package com.myApp;

import android.content.Context;
import android.media.MediaScannerConnection;
import android.net.Uri;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import java.io.File;

public class MediaScannerModule extends ReactContextBaseJavaModule {

    public MediaScannerModule(ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    public String getName() {
        return "MediaScanner";
    }

    @ReactMethod
    public void scanFile(String path) {
        Context context = getReactApplicationContext();

        // Check if the file exists
        File file = new File(path);
        if (!file.exists()) {
            // Notify React Native about the error
            sendEvent("SCAN_FILE_ERROR", "File not found: " + path);
            return;
        }

        // Use MediaScannerConnection to scan the file
        MediaScannerConnection.scanFile(
                context,
                new String[]{path},
                null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    @Override
                    public void onScanCompleted(String path, Uri uri) {
                        // Scanning completed, notify React Native
                        if (uri != null) {
                            sendEvent("SCAN_FILE_SUCCESS", uri.toString());
                        } else {
                            sendEvent("SCAN_FILE_ERROR", "Scan completed, but URI is null");
                        }
                    }
                }
        );
    }

    // Helper method to send events to React Native
    private void sendEvent(String eventName, String message) {
        getReactApplicationContext()
                .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit(eventName, message);
    }
}

来自 MainApplication.java

...
import com.imagewatermarks.MediaScannerPackage;
....
    @Override
        protected List<ReactPackage> getPackages() {
          @SuppressWarnings("UnnecessaryLocalVariable")
          List<ReactPackage> packages = new PackageList(this).getPackages();
            packages.add(MediaScannerPackage());
         
          return packages;
        }
java android react-native module
1个回答
0
投票

添加模块而不是包。

modules.add(new MediaScannerModule(reactContext));

详情请参阅注册模块

© www.soinside.com 2019 - 2024. All rights reserved.