-
Notifications
You must be signed in to change notification settings - Fork 132
MediaStore
0.14.0
Android 10 introduced scoped storage and thus new APIs to store files to Documents, Downloads, Music and other collections. Version 0.14.0 introduced an API to access files in the Media Store but also to create and write to new files in the Media Store. In general you only can access files in the Media Store created by your app, or selected by a picker.
The media store is basically a big database. If you're querying for a file in the media store you'll get a content URI resembling this row.
There is no guarantee that an entry in the media store is resembling an actual file, thus the stat function might fail and not return a path. This is e.g. true for files in the "recently used" section.
So one should always stick to the methods provided by the MediaCollection API and not try to treat resources in the media store like normal files.
0.14.0
Copies an existing file from the internal Storage to the Media Store.
An exmaple for downloading a file and storing it to the downloads
collection
ReactNativeBlobUtil
.config({
fileCache: true
})
.fetch('GET', 'https://example.de/image.png', {'Accept': 'application/octet-stream'}, JSON.stringify(dat))
.then(async (res) => {
let result = await ReactNativeBlobUtil.MediaCollection.copyToMediaStore({
name: filename, // name of the file
parentFolder: '', // subdirectory in the Media Store, e.g. HawkIntech/Files to create a folder HawkIntech with a subfolder Files and save the image within this folder
mimeType: 'image/png' // MIME type of the file
},
'Download', // Media Collection to store the file in ("Audio" | "Image" | "Video" | "Download")
res.path() // Path to the file being copied in the apps own storage
);
});
This example is taking advantage of the fileCache option to directly store the downloaded file and get a path for.
Currently it's not possible to write data directly from a string recevied by fetch, but only to copy it from a file.
0.14.0
Creates a new file in the specified collection without writing any data
let path = await ReactNativeBlobUtil.MediaCollection.createMediafile({
name: filename, // name of the file
parentFolder: '', // subdirectory in the Media Store, e.g. HawkIntech/Files to create a folder HawkIntech with a subfolder Files and save the image within this folder
mimeType: 'image/png' // MIME type of the file
}, 'Download'// Media Collection to store the file in ("Audio" | "Image" | "Video" | "Download")
);
0.14.0
Writes data from a file in the apps storage to an existing entry of the Media Store
await ReactNativeBlobUtil.MediaCollection.writeToMediafile('content://....', // content uri of the entry in the media storage
localpath // path to the file that should be copied
);
0.14.0
Copies an entry form the media storage to the apps internal storage.
This method is very useful if you for example want to upload a file from meda storage to the server. As stated above, rows in the media store are not required to have a path you can use. So the only save way to send a file is by first copying it to the internal storage. (Or use a stream, which currently isn't supported by the lib)
let destpath = ReactNativeBlobUtil.fs.dirs.CacheDir + '/image.png';
await ReactNativeBlobUtil.MediaCollection.copyToInternal('content://....', // content uri of the entry in the media storage
destpath // path to destination the entry should be copied to
);