androidreact-nativeandroid-internal-storage

Error when accessing Android`s storage: java.net.MalformedURLException: no protocol:


I get this error when trying to access app's storage (tried both- private and shared). I tried adding file:// but it doesn`t seem to help.

            String path = getFilesDir().getAbsolutePath() + File.separator + BUNDLE_NAME; // /data/user/0/com.package.vpn/files/rn.bundle
            File file2 = new File(path);

            if (file2 != null && file2.exists()) {
                try {

                    mReactInstanceManager = ReactInstanceManager.builder()
                            .setApplication(getApplication())
                            .setCurrentActivity(currentActivity)
                            .setJSBundleFile(path) // accessing internal storage
                            .addPackages(packages)
                            .setUseDeveloperSupport(BuildConfig.DEBUG)
                            .setInitialLifecycleState(LifecycleState.RESUMED)
                            .build();
                } catch (Exception e2) { }

error:

2022-02-24 13:35:39.466 16294-16799/com.package.vpn E/unknown:ReactNative: Exception in native call
    java.net.MalformedURLException: no protocol: 
        at java.net.URL.<init>(URL.java:601)
        at java.net.URL.<init>(URL.java:498)
        at java.net.URL.<init>(URL.java:447)
        at com.facebook.react.devsupport.DevSupportManagerBase.resetCurrentContext(DevSupportManagerBase.java:707)
        at com.facebook.react.devsupport.DevSupportManagerBase.onNewReactContextCreated(DevSupportManagerBase.java:617)
        at com.facebook.react.ReactInstanceManager.setupReactContext(ReactInstanceManager.java:1126)
        at com.facebook.react.ReactInstanceManager.access$1500(ReactInstanceManager.java:136)
        at com.facebook.react.ReactInstanceManager$5$2.run(ReactInstanceManager.java:1086)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:27)
        at android.os.Looper.loop(Looper.java:237)
        at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:226)
        at java.lang.Thread.run(Thread.java:919)

The file does load as expected(without adding 'file://') but the error keeps happening.


Solution

  • You are missing setJSMainModulePath("index")