question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to copy files from internal storage into scoped storage in an external SD card in the Podcasts directory?

See original GitHub issue

react-native: 0.66.1 react-native-file-access: 2.4.3 Platform: Android 11

Bug I can’t seem to copy files from the local device storage into permission-based scoped storage on an SD card.

I’m trying to save to the Podcasts directory, but I’ve tried many directories on the SD card and get the same failing result.

Details

@alpha0010 sorry as this question involves an additional library (react-native-scoped-storage), and I’m not sure if the problem is some incompatibility between the two. I’m wondering if you could see some obvious mistake I’m making with my process…

I’m trying to copy to the scoped storage “Podcasts” directory on an SD card, but I get an error like follows:

Error: /tree/01F6-AC3F:Podcasts/document/01F6-AC3F:Podcasts/7kJmAzNQ3.mp3: open failed: ENOENT (No such file or directory)

Here’s the sequence of steps I’m doing to get there…

First, I requested permission to the SD card Podcasts directory using:

import * as ScopedStorage from 'react-native-scoped-storage'
...
  _setExtDownloadFileLocationAndroid10 = async () => {
    const dir = await ScopedStorage.openDocumentTree(true)

    if (dir?.uri?.endsWith('%3APodcasts')) {
      const sdCardDownloadLocation = dir.uri
      await AsyncStorage.setItem(PV.Keys.EXT_STORAGE_DLOAD_LOCATION, sdCardDownloadLocation)  
      ...
    }
  ...
}

Then, on file download using react-native-background-downloader, I download to origDestination (RNFS.TemporaryDirectoryPath), then try to move it into scoped storage:

  const folderPath = RNFS.TemporaryDirectoryPath
  const origDestination = `${folderPath}/${episode.id}${ext}`
  // download the episode from origDestination using react-native-background-downloader
  ...
  // then in the `done` callback, I try to move the file into scoped storage:
  const sdDestination = `${sdCardDownloadLocation}/${episode.id}.mp3`
  await FileSystem.cp(origDestination, sdDestination)
...

However…when I do that I get:

Error: /tree/01F6-AC3F:Podcasts/document/01F6-AC3F:Podcasts/7kJmAzNQ3.mp3: open failed: ENOENT (No such file or directory)

When I use RNFS.TemporaryDirectoryPath or RNFS.DocumentDirectoryPath, I have not been able to find where the file is initially stored by react-native-background-downloader (I might just not know where to look). I have used the Files explorer on the device, and I have used Android File Transfer on my Mac to try to find the files…I also have “show hidden files” enabled and still can’t find them.

However, when I use RNFS.DownloadDirectoryPath, I can find the files are successfully downloaded to Internal Storage > Download directory.

In any case, wherever the files are initially downloaded…it seems like according to the error message that react-native-file-access just can’t find or write to the SD Card > Podcasts directory.

There must be something fundamental I’m missing about this process…does that make sense, and do you have any advice on how to accomplish this?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
alpha0010commented, Jul 27, 2022

Should be fixed in v2.4.4.

I would like to add features from libraries such as react-native-scoped-storage and react-native-document-picker to this library, so that only one library is really needed for file handling. However, I just do not have the time for that currently. (PRs always welcome.)

1reaction
alpha0010commented, Jul 22, 2022

From what I can tell, it is an incompatibility. I will see what I can do.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Access media files from shared storage | Android Developers
If scoped storage is enabled, the collection shows only the photos, videos, and audio files that your app has created. Most developers won't...
Read more >
Scoped Storage Tutorial for Android 11: Deep Dive
Create a new directory to migrate the files. To obey scoped storage requirements, place it inside DCIM or Pictures. All the images will...
Read more >
Loading Files From External / Scoped Storage - YouTube
In this video you will learn how we can load photos from external storage into our RecyclerView.⭐ Get certificates for your future job⭐ ......
Read more >
Saving Files in External / Scoped Storage (API 30+) - YouTube
In this video you will learn how you can save photos in external storage.00:00 - Introduction02:37 - Requesting permissions09:21 - Saving ...
Read more >
External storage - File Access with Xamarin.Android
This guide will discuss file access on external storage in Xamarin.Android.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found