react-native-fs icon indicating copy to clipboard operation
react-native-fs copied to clipboard

for existed file 'content://' exists returns false

Open code-by opened this issue 6 years ago • 21 comments

I have image file (obtained by using device camera) with following uri content://media/external/images/media/331 using RNFS.exists.then returns false while file is exists. how to fix this?

code-by avatar Mar 03 '18 18:03 code-by

same here.

baskan avatar Mar 05 '18 14:03 baskan

The "content://"-scheme is currently not supported. I suppose it is android related right? We definitely should add support for it.

itinance avatar Mar 17 '18 18:03 itinance

@itinance yes, in my case it was returning from react-native-image-picker (https://github.com/react-community/react-native-image-picker) on android.

baskan avatar Mar 19 '18 13:03 baskan

Also ran into this issue on Android when using react-native-document-picker (https://github.com/Elyx0/react-native-document-picker)

antsmo avatar Mar 20 '18 14:03 antsmo

Hi did you found any workaround waiting this feature ?

jonathangreco avatar Sep 06 '18 08:09 jonathangreco

Any workaround on that one?

llaine avatar Nov 20 '18 11:11 llaine

@llaine Use RNFS.stat should do the trick.

jonathangreco avatar Nov 20 '18 12:11 jonathangreco

@jonathangreco thank's for your answer. I'm actually trying to use copyFileAssets which throw me an error Error: Asset 'content://media/external/images/media/xx' could not be opened

llaine avatar Nov 20 '18 12:11 llaine

@itinance Hi, is this issue worth to be a top priority ? Because for as long as I recall, neither React-native-fs nor RNFetchBlob have a functionnal exist method for content:// or asset-library://

jonathangreco avatar Nov 27 '18 08:11 jonathangreco

@llaine In fact if you try to RNFS.stata medias in your content:// and it not exist, you'll be able to do your stuff in the catch

jonathangreco avatar Nov 27 '18 08:11 jonathangreco

@jonathangreco The RNFS.stat is throwing the exception. Is there any way to use copyFileAssets on android ?

llaine avatar Nov 28 '18 15:11 llaine

@llaine of course it throws an exception. It's the whole point of this workaround. Catch it and do what you need to do :)

jonathangreco avatar Nov 28 '18 15:11 jonathangreco

Here's a workaround until we get native support:

await Promise.all(arrayOfFilesWithContentUris.map(async (file) => {
  const file = await RNFS.stat(file.uri);
  console.log(file.originalFilepath);
  // will now output a regular file url, compatible with other functions, e.g.:
  // RNFS.unlink(file.originalFilepath);
}));

scarlac avatar May 20 '19 21:05 scarlac

@scarlac thank you, the workaround made it for me 👍

StevenMasini avatar Jun 13 '19 09:06 StevenMasini

@scarlac It's not working for me

07-07 16:14:53.413  5404  7076 I ReactNativeJS: fs stat failed on: content://com.google.android.apps.photos.contentprovider/0/2/mediakey%3A%2Flocal%253A87ecd038-4f96-4c8e-aaf7-4520ad3a6b2c/ORIGINAL/NONE/508284454
07-07 16:14:53.414  5404  7076 I ReactNativeJS: { [Error: Error not specified.]
07-07 16:14:53.414  5404  7076 I ReactNativeJS:   framesToPop: 1,
07-07 16:14:53.414  5404  7076 I ReactNativeJS:   nativeStackAndroid: [],
07-07 16:14:53.414  5404  7076 I ReactNativeJS:   userInfo: null,
07-07 16:14:53.414  5404  7076 I ReactNativeJS:   code: 'EUNSPECIFIED' }

gsunsnackv avatar Jul 07 '19 23:07 gsunsnackv

 RNFS.stat(fileUri).then(fileStat => {
         
});

It just crashes if I try to share image from Photos app, e.g. this one: 'content://com.google.android.apps.photos.contentprovider/1/1/mediakey%3A%2Flocal%253Af752e9f9-7259-4b0a-8ed7-0c8e30f46730/REQUIRE_ORIGINAL/NONE/88846342'

2019-10-18 14:11:30.989 13134-13231/? W/System.err: java.lang.NullPointerException
2019-10-18 14:11:30.989 13134-13231/? W/System.err:     at java.io.File.<init>(File.java:283)
2019-10-18 14:11:30.989 13134-13231/? W/System.err:     at com.rnfs.RNFSManager.stat(RNFSManager.java:626)
2019-10-18 14:11:30.989 13134-13231/? W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2019-10-18 14:11:30.989 13134-13231/? W/System.err:     at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:371)
2019-10-18 14:11:30.989 13134-13231/? W/System.err:     at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:150)
2019-10-18 14:11:30.989 13134-13231/? W/System.err:     at com.facebook.react.bridge.queue.NativeRunnable.run(Native Method)
2019-10-18 14:11:30.989 13134-13231/? W/System.err:     at android.os.Handler.handleCallback(Handler.java:873)
2019-10-18 14:11:30.989 13134-13231/? W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:99)
2019-10-18 14:11:30.989 13134-13231/? W/System.err:     at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:26)
2019-10-18 14:11:30.989 13134-13231/? W/System.err:     at android.os.Looper.loop(Looper.java:193)
2019-10-18 14:11:30.989 13134-13231/? W/System.err:     at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:225)
2019-10-18 14:11:30.989 13134-13231/? W/System.err:     at java.lang.Thread.run(Thread.java:764)

DaveLomber avatar Oct 18 '19 11:10 DaveLomber

any chance this is going to be supported? Seems silly to not support content:// for android on a cross-platform architecture

scousino avatar Dec 09 '20 17:12 scousino

I ran into this issue recently when trying to upload a file that was shared to my app (via the react-native-share-menu module). The file that it was trying to upload started with content:// and so while RNFS.stat as well as RNFS.uploadFiles threw an exception when trying to handle that file, RNFS.copyFile could work with it just fine. So here's my workaround:

let fileName = sharedItem.data.split('\\').pop().split('/').pop()
let filePath = sharedItem.data
        
if (Platform.OS === 'android') {
  try {
    let ext = null

    switch(sharedItem.mimeType) {
      case 'image/jpeg':
        ext = 'jpg'
        break
      case 'image/png':
        ext = 'png'
        break
      default:
        Alert.alert('Unsupported file type.')
        this.setState({ loading: false })
        return
    }

    fileName = fileName + '.' + ext
    filePath = RNFS.DocumentDirectoryPath + '/' + fileName

    await RNFS.copyFile(sharedItem.data, filePath)
  } catch (err) {
    log('File Read Error:', err)
    Alert.alert('An unknown error occured while reading your file.')
    this.setState({ loading: false })
    return
  }
}

const files = [
  {
    name: 'image',
    filename: fileName,
    filepath: filePath,
    filetype: sharedItem.mimeType
  }
]

// and then RNFS.uploadFiles as usual

Basically, I'm just copying the file over to my app's own directory for handling. But with the web server I was uploading the images to, the file extension is required and in this case the file extension wasn't in the file path, luckily the share module gives me the MIME type and my application only supports a limited number of types so I was able to make do.

kodie avatar Aug 31 '21 16:08 kodie

Can we get any sort of traction on this??? This seems like a easily fixed oversight to not account for content://

scousino avatar Sep 07 '21 17:09 scousino

@scarlac calling const file = await RNFS.stat("content://..."); still returns the following error {"nativeStackAndroid":[],"userInfo":null,"message":"File does not exist","code":"EUNSPECIFIED"} even tho it was picked right from the device gallery.

File exists because calling for the same file RNFS.moveFile("content://...", to) works fine.

To reproduce, I just pick a video media on Android using

import {launchImageLibrary} from 'react-native-image-picker'; //v^4.8.5
const result = await launchImageLibrary({
    mediaType: 'mixed',
    videoQuality: 'high',
    quality: 1,
    selectionLimit: 10,
});

charlyBerthet avatar Aug 26 '22 08:08 charlyBerthet

Hi. Just commenting here because we found the issue on our app in a native Android context. I'm unsure if this will be helpful but there's only a handful of places on the web that seem to be highlighting the error

For apps using the photopicker, there is something about the implementation that causes DocumentFile.exists() (and possibly File.exists()) to return false when a "content://" URI is returned

Instead what we use to check validity is DocumentFile.name != null

Sorry it's not a ReactNative fix for you but perhaps a fix for your React Native library can be pulled from that knowledge

kassim avatar May 30 '24 15:05 kassim