Skip to content

Fails to open a file after creating it. #25502

Closed
@Evonoucono

Description

@Evonoucono

I am trying to create a new file and then immediately open it after creation using another app of choice. Regardless the app or file type, the selected app will fail to read the file (as if it were corrupted). I can open the file manually from android file explorer just fine, and after doing so my app can also open the file. But once a new file is created I have to open it manually again to view it from another app.

I can read the contents of the file just fine after creation, but other apps can't after using Linking.openURL.

React Native version:
react-native-cli: 2.0.1
react-native: 0.59.9
npm: 6.4.1
Android Studio: Version 3.4.0.0 AI-183.6156.11.34.5522156

Steps To Reproduce, I've only tested on Android:

  1. Use react-native-fs to create a file (I've tried txt, html, and csv)
  2. Using Linking.openURL to open the just created file
  3. Choose an app to open the file (when prompted by the os)
  4. The chosen app will fail to read the file 100% of the time (for me at least?)

Other apps should be able to read the newly created file when opened from my app using Linking.openURL(). But, for example, microsoft excel will say "Can't open file. Try saving the file on the device and then opening it"

I'm not sure if the directories in the following code are specific to my device (especially the readFrom directory) but I have no other idea of how to access the files in Linking.openUrl

        var fileName = "someCsvFile.csv";
        var content = "One, Two, Three, Four \n 1, 2, 3, 4";
        var toFolder = RNFS.ExternalStorageDirectoryPath + '/Download/';
        var readFrom = "content://com.sec.android.app.myfiles.FileProvider/device_storage/Download/" + fileName;
        
        RNFS.writeFile(toFolder + fileName, content, 'utf8').then((success) => {
            
            Linking.canOpenURL(readFrom) //DOES WORK ON MY DEVICE
            .then((supported) => {
                if (supported) { //ALWAYS RETURNS TRUE ON MY DEVICE
                    Linking.openURL(readFrom); //WORKS, but other app fails to read file
                }
            }).catch((err) => {});
            
        }).catch((err) => {});

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions