Rn-fetch-blob: App crashes while reading file on android, Error: java.lang.NumberFormatException

Created on 18 Jun 2018  路  8Comments  路  Source: joltup/rn-fetch-blob

Versions:

"react-native-fetch-blob": "0.10.8"
"react-native": "0.49.5"

I was reading a file using this code:

RNFetchBlob.fs
      .readFile(filePath, "ascii")
      .then(data => {
        this.showProgressModal = true;
        ...
      }).catch(e){}

On Android device I got this error and app crashes:

java.lang.NumberFormatException: For input string: "raw:/storage/emulated/0/Download/Bittrexv1.csv"
                                                                             at java.lang.Long.parseLong(Long.java:590)
                                                                             at java.lang.Long.valueOf(Long.java:804)
                                                                             at com.RNFetchBlob.Utils.PathResolver.getRealPathFromURI(PathResolver.java:41)
                                                                             at com.RNFetchBlob.RNFetchBlobFS.normalizePath(RNFetchBlobFS.java:918)
                                                                             at com.RNFetchBlob.RNFetchBlobFS.readFile(RNFetchBlobFS.java:141)
                                                                             at com.RNFetchBlob.RNFetchBlob$6.run(RNFetchBlob.java:208)
                                                                             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
                                                                             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
                                                                             at java.lang.Thread.run(Thread.java:764)

PS. Its working fine on Android emulator and ios simulator. Haven't tested on ios device yet. Please let me know if more information is needed to debug the issue, Thanks!

question

Most helpful comment

simple workaround just for now.

const filePath = item.uri.split('raw%3A')[1].replace(/\%2F/gm, '/');
RNFetchBlob.fs.readFile(filePath, 'utf8').then(...)

All 8 comments

Saw this behaviour if it is of any help in debugging, when I moved the above file Bittrexv1.csv from my downloads folder to google drive folder and read from there then it works fine. I am using react-native-document-picker to select file. I printed the filePath that I was passing in to readFile method, it is : content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2FBittrexv1.csv

Since you didn't, can you first try on device and check if the same happens?

This file path raw:/storage/emulated is a file path that you would only have on an emulator, you would never get this path on a real device.
Is this path hardcoded somewhere or being saved somehow? Since the file paths will be different depending on whether you're running on a real device or an emulator, you should dynamically look up the directory using the dirs constants (like DocumentDir for example). If you are instead downloading the file and then failing to find it, are you using the file path returned after you finish downloading?

Getting this crash as well:

10-07 08:59:41.255 20988 21020 I ReactNativeJS: 2018-10-07 08:59:41: "Got file: content://com.android.providers.downloads.documents/document/raw%3A%2Fstorage%2Femulated%2F0%2FDownload%2F05_07_2018.pdf"

10-07 08:59:41.284 20988 21094 E AndroidRuntime: FATAL EXCEPTION: pool-1-thread-1
10-07 08:59:41.284 20988 21094 E AndroidRuntime: Process: net.cozic.joplin, PID: 20988
10-07 08:59:41.284 20988 21094 E AndroidRuntime: java.lang.NumberFormatException: For input string: "raw:/storage/emulated/0/Download/05_07_2018.pdf"
10-07 08:59:41.284 20988 21094 E AndroidRuntime:        at java.lang.Long.parseLong(Long.java:590)
10-07 08:59:41.284 20988 21094 E AndroidRuntime:        at java.lang.Long.valueOf(Long.java:804)
10-07 08:59:41.284 20988 21094 E AndroidRuntime:        at com.RNFetchBlob.Utils.PathResolver.getRealPathFromURI(PathResolver.java:41)
10-07 08:59:41.284 20988 21094 E AndroidRuntime:        at com.RNFetchBlob.RNFetchBlobFS.normalizePath(RNFetchBlobFS.java:918)
10-07 08:59:41.284 20988 21094 E AndroidRuntime:        at com.RNFetchBlob.RNFetchBlobFS.cp(RNFetchBlobFS.java:450)
10-07 08:59:41.284 20988 21094 E AndroidRuntime:        at com.RNFetchBlob.RNFetchBlob$5.run(RNFetchBlob.java:167)
10-07 08:59:41.284 20988 21094 E AndroidRuntime:        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
10-07 08:59:41.284 20988 21094 E AndroidRuntime:        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
10-07 08:59:41.284 20988 21094 E AndroidRuntime:        at java.lang.Thread.run(Thread.java:764)Crash attachment

The path in this case is raw:/storage/emulated/0/Download/05_07_2018.pdf, which is a path returned by react-native-document-picker. The path exists though I'm not sure if the "raw:" part is valid, but regardless an invalid path should not make the app crash.

I think #226 should fix this

simple workaround just for now.

const filePath = item.uri.split('raw%3A')[1].replace(/\%2F/gm, '/');
RNFetchBlob.fs.readFile(filePath, 'utf8').then(...)

I am still facing this issue in "rn-fetch-blob": "^0.10.15" is it not yet merged to master ?

03-07 09:13:09.434 25287 25395 E AndroidRuntime: java.lang.NumberFormatException: For input string: "raw:/storage/emulated/0/Download/patients.json"
03-07 09:13:09.434 25287 25395 E AndroidRuntime:    at java.lang.Long.parseLong(Long.java:590)
03-07 09:13:09.434 25287 25395 E AndroidRuntime:    at java.lang.Long.valueOf(Long.java:804)
03-07 09:13:09.434 25287 25395 E AndroidRuntime:    at com.RNFetchBlob.Utils.PathResolver.getRealPathFromURI(PathResolver.java:43)
03-07 09:13:09.434 25287 25395 E AndroidRuntime:    at com.RNFetchBlob.RNFetchBlobFS.normalizePath(RNFetchBlobFS.java:1126)
03-07 09:13:09.434 25287 25395 E AndroidRuntime:    at com.RNFetchBlob.RNFetchBlobFS.readFile(RNFetchBlobFS.java:154)
03-07 09:13:09.434 25287 25395 E AndroidRuntime:    at com.RNFetchBlob.RNFetchBlob$6.run(RNFetchBlob.java:226)
03-07 09:13:09.434 25287 25395 E AndroidRuntime:    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
03-07 09:13:09.434 25287 25395 E AndroidRuntime:    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
03-07 09:13:09.434 25287 25395 E AndroidRuntime:    at java.lang.Thread.run(Thread.java:764)

@navneet-kumar It got merged into 0.10.9 in September, but not master. I am not sure why, the fix has been there since last November and just got merged (into the wrong branch) last month.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

baxri picture baxri  路  22Comments

tzwickl picture tzwickl  路  14Comments

meiqi1992 picture meiqi1992  路  19Comments

fgagneten picture fgagneten  路  23Comments

viv3kk picture viv3kk  路  13Comments