Skip to content
This repository has been archived by the owner on Mar 16, 2019. It is now read-only.

fixed getExternalFilesDir null issue #557

Closed
wants to merge 5 commits into from
Closed

fixed getExternalFilesDir null issue #557

wants to merge 5 commits into from

Conversation

phodal
Copy link

@phodal phodal commented Oct 12, 2017

this issue has happened in a cloud tests' machines, here was log in phodal/play-app#1

10-11 20:42:05.803 31781 31781 E unknown:ReactNative: Got DOWN touch before receiving UP or CANCEL from last gesture
10-11 20:42:05.995 31781 31781 V ActivityThread: Finishing stop of ActivityRecord{9b47a49 token=android.os.BinderProxy@3c907f4e {com.phodal.play/com.phodal.play.MainActivity}}: show=false win=com.android.internal.policy.impl.MiuiPhoneWindow@1eafb3c4
10-11 20:42:05.995 31781 31781 V PhoneWindow: DecorView setVisiblity: visibility = 4 ,Parent =ViewRoot{3bd3f9ac com.phodal.play/com.phodal.play.MainActivity,ident = 0}, this =com.android.internal.policy.impl.PhoneWindow$DecorView{af9425a I.E..... R....... 0,0-1080,1920}
10-11 20:42:06.111 31781 31781 E unknown:ReactNative: Got DOWN touch before receiving UP or CANCEL from last gesture
10-11 20:42:06.226 31781 31811 D BluetoothAdapter: getName
10-11 20:42:06.237 31781 31811 V SettingsInterface: invalidate [secure]: current 14 != cached 0
10-11 20:42:06.363 31781 31811 W ContextImpl: Failed to ensure directory: /storage/emulated/0/Android/data/com.phodal.play/files
10-11 20:42:06.367 31781 31811 E ReactNativeJS: Java exception in 'NativeModules'
10-11 20:42:06.367 31781 31811 E ReactNativeJS:
10-11 20:42:06.367 31781 31811 E ReactNativeJS: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File java.io.File.getParentFile()' on a null object reference
10-11 20:42:06.377 31781 31811 E ReactNativeJS: Module AppRegistry is not a registered callable module (calling runApplication)
10-11 20:42:06.378 31781 31811 E ReactNativeJS: Module AppRegistry is not a registered callable module (calling runApplication)
10-11 20:42:06.384 31781 31812 E AndroidRuntime: FATAL EXCEPTION: mqt_native_modules
10-11 20:42:06.384 31781 31812 E AndroidRuntime: Process: com.phodal.play, PID: 31781
10-11 20:42:06.384 31781 31812 E AndroidRuntime: com.facebook.react.common.JavascriptException: Java exception in 'NativeModules'
10-11 20:42:06.384 31781 31812 E AndroidRuntime:
10-11 20:42:06.384 31781 31812 E AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File java.io.File.getParentFile()' on a null object reference, stack:
10-11 20:42:06.384 31781 31812 E AndroidRuntime: com.RNFetchBlob.RNFetchBlobFS.getSystemfolders@216
10-11 20:42:06.384 31781 31812 E AndroidRuntime: com.RNFetchBlob.RNFetchBlob.getConstants@84
10-11 20:42:06.384 31781 31812 E AndroidRuntime: com.facebook.react.bridge.JavaModuleWrapper.getConstants@140
10-11 20:42:06.384 31781 31812 E AndroidRuntime: android.os.Handler.handleCallback@815
10-11 20:42:06.384 31781 31812 E AndroidRuntime: android.os.Handler.dispatchMessage@104
10-11 20:42:06.384 31781 31812 E AndroidRuntime: com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage@31
10-11 20:42:06.384 31781 31812 E AndroidRuntime: <unknown>@856:101
10-11 20:42:06.384 31781 31812 E AndroidRuntime: i@2:565
10-11 20:42:06.384 31781 31812 E AndroidRuntime: n@2:348
10-11 20:42:06.384 31781 31812 E AndroidRuntime: t@2:210
10-11 20:42:06.384 31781 31812 E AndroidRuntime: <unknown>@855:3413
10-11 20:42:06.384 31781 31812 E AndroidRuntime: i@2:565
10-11 20:42:06.384 31781 31812 E AndroidRuntime: n@2:348
10-11 20:42:06.384 31781 31812 E AndroidRuntime: t@2:210
10-11 20:42:06.384 31781 31812 E AndroidRuntime: <unknown>@852:3421
10-11 20:42:06.384 31781 31812 E AndroidRuntime: i@2:565
10-11 20:42:06.384 31781 31812 E AndroidRuntime: n@2:348
10-11 20:42:06.384 31781 31812 E AndroidRuntime: t@2:210
10-11 20:42:06.384 31781 31812 E AndroidRuntime: <unknown>@851:196
10-11 20:42:06.384 31781 31812 E AndroidRuntime: i@2:565
10-11 20:42:06.384 31781 31812 E AndroidRuntime: n@2:348
10-11 20:42:06.384 31781 31812 E AndroidRuntime: t@2:210
10-11 20:42:06.384 31781 31812 E AndroidRuntime: <unknown>@850:135
10-11 20:42:06.384 31781 31812 E AndroidRuntime: i@2:565
10-11 20:42:06.384 31781 31812 E AndroidRuntime: n@2:348
10-11 20:42:06.384 31781 31812 E AndroidRuntime: t@2:210
10-11 20:42:06.384 31781 31812 E AndroidRuntime: <unknown>@485:467
10-11 20:42:06.384 31781 31812 E AndroidRuntime: i@2:565
10-11 20:42:06.384 31781 31812 E AndroidRuntime: n@2:348
10-11 20:42:06.384 31781 31812 E AndroidRuntime: t@2:210
10-11 20:42:06.384 31781 31812 E AndroidRuntime: <unknown>@451:341
10-11 20:42:06.384 31781 31812 E AndroidRuntime: i@2:565
10-11 20:42:06.384 31781 31812 E AndroidRuntime: n@2:348
10-11 20:42:06.384 31781 31812 E AndroidRuntime: t@2:210
10-11 20:42:06.384 31781 31812 E AndroidRuntime: <unknown>@448:267
10-11 20:42:06.384 31781 31812 E AndroidRuntime: i@2:565
10-11 20:42:06.384 31781 31812 E AndroidRuntime: n@2:348
10-11 20:42:06.384 31781 31812 E AndroidRuntime: t@2:210
10-11 20:42:06.384 31781 31812 E AndroidRuntime: <unknown>@59:184
10-11 20:42:06.384 31781 31812 E AndroidRuntime: i@2:565
10-11 20:42:06.384 31781 31812 E AndroidRuntime: n@2:348
10-11 20:42:06.384 31781 31812 E AndroidRuntime: t@2:210
10-11 20:42:06.384 31781 31812 E AndroidRuntime: <unknown>@12:38
10-11 20:42:06.384 31781 31812 E AndroidRuntime: i@2:565
10-11 20:42:06.384 31781 31812 E AndroidRuntime: n@2:278
10-11 20:42:06.384 31781 31812 E AndroidRuntime: t@2:210
10-11 20:42:06.384 31781 31812 E AndroidRuntime: global code@898:9
10-11 20:42:06.384 31781 31812 E AndroidRuntime:
10-11 20:42:06.384 31781 31812 E AndroidRuntime: at com.facebook.react.modules.core.ExceptionsManagerModule.showOrThrowError(ExceptionsManagerModule.java:56)
10-11 20:42:06.384 31781 31812 E AndroidRuntime: at com.facebook.react.modules.core.ExceptionsManagerModule.reportFatalException(ExceptionsManagerModule.java:40)
10-11 20:42:06.384 31781 31812 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-11 20:42:06.384 31781 31812 E AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-11 20:42:06.384 31781 31812 E AndroidRuntime: at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:363)
10-11 20:42:06.384 31781 31812 E AndroidRuntime: at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:162)
10-11 20:42:06.384 31781 31812 E AndroidRuntime: at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
10-11 20:42:06.384 31781 31812 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:815)
10-11 20:42:06.384 31781 31812 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:104)
10-11 20:42:06.384 31781 31812 E AndroidRuntime: at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:31)
10-11 20:42:06.384 31781 31812 E AndroidRuntime: at android.os.Looper.loop(Looper.java:194)
10-11 20:42:06.384 31781 31812 E AndroidRuntime: at com.facebook.react.bridge.queue.MessageQueueThreadImpl$3.run(MessageQueueThreadImpl.java:194)
10-11 20:42:06.384 31781 31812 E AndroidRuntime: at java.lang.Thread.run(Thread.java:818)

when I search getExternalFilesDir null in Google, it seems it's common problem. I don't very sure the path is correctly ? but just want to confirm it.

It seems in a error branch..

@cbjs
Copy link

cbjs commented Oct 24, 2017

+1

@cbjs cbjs mentioned this pull request Oct 24, 2017
@sivakumar-cf
Copy link

sivakumar-cf commented Dec 4, 2017

Can you please merge this?

if(externalFilesDir != null) {
res.put("SDCardApplicationDir", externalFilesDir.getParentFile().getAbsolutePath());
} else {
res.put("SDCardApplicationDir", Environment.getExternalStorageDirectory().getAbsolutePath());
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getExternalStorageDirectory() is the root directory of sd card and files stored there will not be deleted when application is uninstalled. Not sure if this is what we want to do, put some application files in a root of sdcard.

The other thing is that getSystemfolders() is used by getConstants() which are pre-defined keys exposed to JavaScript, and I'm sure they are not updated during runtime, only taken once on module load (they're constants right :) ). So external storage state can change and getExternalStorageDirectory() or getExternalFilesDir() should be invoked when needed, something like:

(not tested)
RNFetchBlobFS.java

    static public void getSDCardDir(Callback callback) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            callback.invoke(Environment.getExternalStorageDirectory().getAbsolutePath());
        } else {
            callback.invoke(null, "Media storage not mounted");
        }
    }

    static public void getSDCardApplicationDir(ReactApplicationContext ctx, Callback callback) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            try {
                callback.invoke(ctx.getExternalFilesDir(null).getParentFile().getAbsolutePath());
            } catch (Exception e) {
                callback.invoke(null, e.getLocalizedMessage());
            }
        } else {
                callback.invoke(null, "Media storage not mounted");
        }
    }

RNFetchBlob.java

    @ReactMethod
    public void getSDCardDir(Callback callback) {
        RNFetchBlobFS.getSDCardDir(callback);
    }

    @ReactMethod
    public void getSDCardApplicationDir(Callback callback) {
        RNFetchBlobFS.getSDCardApplicationDir(this.getReactApplicationContext(), callback);
    }

What do you think @phodal ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your change is make sense.

Actually, I don't have to many experience on Android development. This PR just want to mention people that here was a problem. And, I search google for this PR, it fixed my issue.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, it will fix the NullPointerException. Do you plan to update the PR then or want me to test my stuff on Android and make new PR?

Copy link

@chrusart chrusart Dec 5, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And one concern is that getSDCardDir() and getSDCardApplicationDir() methods doesn't fit to RNFetchBlob class, but the maintainer must put two cents to it.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do agree with @chrusart change. @phodal Please make the suggested change.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sivakumar-cf In my option , i think @chrusart can create another PR, and I will closed this one.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will make a PR, wip :)

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR #619 created.

@chrusart
Copy link

chrusart commented Dec 5, 2017

@phodal @cbjs @sivakumar-cf , please check my comment, we also need just null check to rid off the crashes, but there could be other issues later with this part of code (left files in root sd card folder after app uninstall, reading already unmounted sd card, etc).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants