Content URI support#395
Conversation
# New methods * `getFileUri()` that returns Uri for any passed path (with or without file:// prefix or with content:// prefix) * `getInputStream()` that returns InputStream for any Uri passed * `getOutputStream()` that returns OutputStream for any Uri passed * `getInputStreamBytes()` that reads the whole content of an InputStream and returns it as a byte array # Modified methods Added content uri support to: * `readFile()` * `writeFile()` * `appendFile()` * `write()` - Note: random access (position>=0) is not supported for content uris * `read()` * `copyFile()` - both source and destination can be content uris * `copyInputStream()` - output stream can be a content uri # Others * IORejectionException - an exception that accepts a code besides a message
|
Looks good, but can I ask why the custom exception class? |
|
I added it to preserve an error code (ENOENT/EISDIR) in rejected promise, because I moved error handling to other methods that do not take Promise as an input. I'd be glad to hear if there's a better way to do it. |
|
I actually like what you did a lot. I think it cleans ups and makes everything a bit more elegant. I was just curious about the thought behind making the change. |
|
thank you very much! |
|
I am still getting an error of "Permission Denial" for the content uri with copyFile(). the stacktrace for the error is like:
and the exception thrown appears to be: |
|
@ankag94 I have tested this PR on several Android versions (API 23, 25). An exception you're getting may be caused by calling |
|
This is working great for us to copy image URLs returned from the camera roll. Thank you so much! |
|
I'm trying to use this method getInputStream() but I have this error: '_reactNativeFs2.default.getInputStream is not a function' and my code looks like this: RNFS.getInputStream(uri).then({...}).catch({...}) |
|
@annammarri |
|
Please is it possible to get the actual path using this package? |
|
I want to read text from text file placed in the 'files' folder of the app directory Android. Not Working!!! |
|
@imran1992, please see what |
|
Thanks @krzysztof-miemiec ITS Working ..... content://com.android.externalstorage.documents/document/primary%3AAndroid%2Fdata%2Fcom.smsappt%2Ffiles%2Frn.txtI received it from |
|
@imran1992 Hey, I'm using |
|
I'm using this lib, and want to get content:// provider, but it occur error "Folder does not exist" |
|
Hi, wondering if there is any solution for this? I'm also stuck where my simple content:// is getting file not found on a RNFS.stat Example of my content URI: content://com.android.providers.downloads.documents/document/590 This is coming from react-native-document-picker |
|
Hi I'm also having problems with this. How does one convert the content uri to the proper full path? Here is an example of the kind of path I'm dealing with: content://com.android.providers.media.documents/document/image%3A19030 |
|
with rn fetch blob i read the file and then write to directory, however, for large pdf files this fails. So I am trying to figure out how to create a blob from this content uri? |
|
Did any one find solution for converting uri to actual path? |
I ended up having to use import RNFS from 'react-native-fs';
...
const destPath = `${RNFS.TemporaryDirectoryPath}/${shortid.generate()}`;
await RNFS.copyFile(selectedDocument.uri, destPath);
console.log(await RNFS.stat(destPath); |
|
this may help you https://stackoverflow.com/a/62720645/9894200 |
hello @codetheweb , where |
|
@muhammad-ihsan updated the code example. |
|
@codetheweb But I guess that you lose the original "creation date" :( |
|
I'm trying to use the getFileUri() method mentioned here but have no idea how to use it.
|
|
`const contentResolvedData = await RNFS.stat(uri); let path = const ext = file_name?.split('.').pop(); |
Unable to get this to work because of permission error. It works locally, even on release but when i put it on APK it dosen't work. Am on API 29 'Error: ENOENT: /storage/emulated/0/Download/xxxx.pdf: open failed: EACCES (Permission denied), open '/storage/emulated/0/Download/xxx.pdf'' I think RNFS don't work with API 29 for files in the download folder anymore? I checked that the read/write external storage is granted, and have the requestLegacyExternalStorage="true" on as well (which i suspect is not working on APK/Playstore) Correspondingly, the RNFS.upload dosen't work also because its getting socket closed (not able to pick up the file) |
|
I am able to use save/download to the download folder. Extract from gradle: Download function: Permissions requested: Manifest permissions: I hope this helps? |
|
Thanks for reverting so quickly @marcodafonseca |
|
No worries, @enigmablue Let me know how your new repo test goes. |
|
Unfortunately still dosen't work :( @marcodafonseca https://github.com/EnigmaGlobe/rnfspdftest/tree/master (ps: if i'm not wrong, setting requestLegacyExternalStorage works on testing but fails on APK/playstore) |
|
Found the solution, just needed base64 encoding to read the pdf file. And copyfile wouldn't work in that context because it didn't have encoding in the input. |
This PR is based on #49, I used the same way of getting
InputStreams/OutputStreams as @jrichardlai. It should fix #351.New methods
getFileUri()that returns Uri for any passed path (with or without file:// prefix or with content:// prefix)getInputStream()that returns InputStream for any Uri passedgetOutputStream()that returns OutputStream for any Uri passedgetInputStreamBytes()that reads the whole content of an InputStream and returns it as a byte arrayModified methods
Added content uri support to:
readFile()writeFile()appendFile()write()- Note: random access (position>=0) is not supported for content urisread()copyFile()- both source and destination can be content uriscopyInputStream()- output stream can be a content uriOthers