achorein / expo-share-intent

🚀 Simple share intent in an Expo Native Module
MIT License
119 stars 10 forks source link

How to actually read a shared file into memory? #71

Closed zholmes1 closed 2 weeks ago

zholmes1 commented 3 weeks ago

Hey thanks so much for this repo. I was able to add sharing intent to my app, and I can display the shared image in an Image component.

But I can't figure out how to actually read this file! It's not in the examples anywhere.

I'd like to get the image into memory in Base64. I tried ExpoFileSystem.readAsStringAsync but it says the content:// schema that I get from the share intent file path is invalid.

Any help would be much appreciated!

zholmes1 commented 2 weeks ago

It looks like it's handled differently on iOS and Android. (file.path works for iOS, cacheDirectory + file.fileName works for Android)

I wrote up a little piece of code that checks various places hoping that one of them works.

  const possiblePaths = [
    FS.cacheDirectory + file.fileName,
    file.path,
    FS.documentDirectory + file.fileName
  ]

  let path
  for (const possiblePath of possiblePaths) {
    console.log('checking', possiblePath)
    try {
      const { exists } = await FS.getInfoAsync(possiblePath)
      if (exists) {
        path = possiblePath
        console.log('correct!', Platform.OS)
        break
      }
    } catch (e) {
      console.error(e)
    }
  }

  if (!path) {
    throw new Error(
      `We were unable to read this file. (${file.path} ${file.mimeType} ${file.fileName})`
    )
  }
achorein commented 2 weeks ago

ok maybe something to do with the file path returned by share intent on android.

achorein commented 2 weeks ago

@zholmes1 Android file path fixed in v2.1.1 !