I am working on a react native project. I need to store the captured images in the custom folder.
I am using react native fs library for that. I am able to create the image in the desired directory but I am not able to see those images in my iphones' file directory.
Here is my code I am using to store the images.
async moveAttachment(capturedImagePath) {
$filePathDir = `${RNFS.DocumentDirectoryPath}/myapp/myfilename`;
$filePath = `${$filePathDir }/myfilename.png`;
return new Promise((resolve, reject) => {
RNFS.mkdir(filePathDir)
.then(() => {
RNFS.moveFile(capturedImagePath, filePath )
.then(() => resolve(dirPictures))
.catch(error => reject(error));
})
.catch(err => reject(err));
});
}
I am able to see the image in my simulator's document directory but not able to see in the iPhone > files directory.
Please help me to figure this out.
You should be able to enable it by updating your Info.plist. You need to add two keys:
UIFileSharingEnabled and LSSupportsOpeningDocumentsInPlace should both be added and set to YES.
UIFileSharingEnabled: Application supports iTunes file sharing
LSSupportsOpeningDocumentsInPlace: Supports opening documents in place
This will allow your DocumentsDirectory to be opened in iTunes and it should also allow you to share your files via the Files application.
You can read more about LSSupportsOpeningDocumentsInPlace here
In iOS 11 and later, if both this key and the UIFileSharingEnabled key
are YES, the local file provider grants access to all the documents in
the app’s Documents directory. These documents appear in the Files
app, and in a document browser. Users can open and edit these document
in place.
Note that any item you save in the Documents directory will be accessible.
Related
I'm using ipfs to download files... but ipfs does not have filenames or extensions.
How can I properly set the extension for the saved file, based on the downloaded data?
You can add files with ipfs add -w to "wrap" them in a directory. This will preserve the filename.
There are also plans to record metadata like mime types along with files but, unfortunately, progress has gotten a bit stuck on this front.
There is no way to conclusively get the file extension in IPFS, so instead, you can look at the contents of the file to infer its file type (using file-type, for example)
When you have the full contents of the file in a buffer, you can do:
import {fileTypeFromBuffer} from 'file-type';
let buffer = /* buffer of file from IPFS */;
// undefined or string of file extension
let ext = (await fileTypeFromBuffer(buffer))?.ext;
If you wan't to do this in a React app on the frontend this works :
let contentType = await fetch(imageUrl)
.then(response => {
return response.blob().then(blob => {
return {
contentType: response.headers.get("Content-Type"),
raw: blob
}
})
})
.then(data => {
return data.contentType
})
file-type is intended for use in Node apps, not on the client side.
If you try to use a library intended for Node then you will get errors that relate to the library's internal dependencies on built-in Node modules which are not available in the browser. You should use a different library for making HTTP requests from the browser; I'd suggest using the browser's built-in fetch.
you must get the program file type, then import the extension name on the program name you downloaded from the buffer
i hope you understand this
am using React Native Expo and I was browsing the web to find a way to download assets, and files to my react native project and came across with this post How to Download image in react native
When the user clicks the download button I want assets/files to be downloaded and stored inside the app not directly on the user's phone. I mean I don't want the users to view the downloaded files or delete them manually.
I just want the downloaded assets/files to be accessible by the React Native app. Am doing this to make the app work offline.
Once the users downloaded the assets/files, the app can use the downloaded assets/files. How can I accomplish that?
Thank you in advance!
If you are using expo managed workflow, then rn-fetch-blob will not work for you.
In that case, Expo File System is probably your way to go.
Firstly, install expo-file-system. See this
Next, for saving files and not letting users delete them manually, store them inside the cache-directory like this
import * as FileSystem from 'expo-file-system';
const downloadAssets = async () => {
let name = "Samplefile.jpg";
const result = FileSystem.createDownloadResumable(
url_Of_the_File_You_Want_to_Download,
FileSystem.cacheDirectory + name
);
const response = await result.downloadAsync();
if (response.status === 200) {
// File successfully saved
} else {
// Some error
}
};
To access this file in your app simple execute this function
import * as FileSystem from 'expo-file-system';
const getFiles = async () => {
const CacheDir = await FileSystem.readDirectoryAsync(
FileSystem.cacheDirectory
);
console.log(CacheDir); //Files are stored here
};
I'm trying to delete some files from the Android Download Directory.
I got the android permission to the external storage to do it, so this is my code :
var RNFS = require('react-native-fs');
...
await RNFS.exists(filepath).then((exists) => {
if (exists) {
RNFS.unlink(filepath).then(() => {
RNFS.scanFile(filepath);
})
}
});
The unlink function works perfectly when I add some log in it. It doesn't catch(), but when I launch the same script again, the unlink still works while the file must be deleted, how is it possible ??
I don't understand what I have to do to completely delete these files...
EDIT :
I've checked the react-native-fs and it says :
Android support is currently limited to only the DocumentDirectory. This maps to the app's files directory.
So how can I delete files in the Download directory ?
I am currently trying to open an image that's saved in the cache from the react-native-camera module. According to this Open android Gallery App from react native app they managed to do it when they passed in the content:// url instead of the file:// url, but after some research I can't find anything on converting file uris to content - only the opposite. I can get the image file paths by:
import RNFS from 'react-native-fs'
import { Linking } from 'react-native'
RNFS.readDir(RNFS.CachesDirectoryPath)
.then(arr => RNFS.readDir(arr[0].path)) // The Camera directory
.then(arr => arr.forEach(item => {
const contentURI = somehowConvertPathToContentURI(item.path)
Linking.canOpenURL(contentURI)
.then(able => able ? Linking.openURL(contentURI) : console.log('No application available'))
.catch(console.log)
}))
.catch(console.log)
A logged path would be /data/user/0/com.myapp/cache/Camera/ad8db2ca-4739-47cc-b18d-c5147c8c26e0.jpg. Adding file:// in front will give me the file uri, what about content://?
As a side note, I'm able to directly show the image if I read and convert it to base64 with react-native-fs, then providing the base64 to react-native's <Image> component - but I'd like to open the app with a gallery application
I am writing an react-native ios application which generates text file. The file is stored at /var/mobile/Containers/Data/Application/76C594FD-282D-41B2-9BE6-B6B1C785BDE6/Documents/backup.json using react-native-fs.
I want to share this file to google drive/dropbox/microsoft one drive so I use the ActionSheetIOS.showShareActionSheetWithOptions API to share the file:
ActionSheetIOS.showShareActionSheetWithOptions({
url: '/var/mobile/Containers/Data/Application/76C594FD-282D-41B2-9BE6-B6B1C785BDE6/Documents/backup.json'
},
(error) => console.warn(error.message),
(success, method) => {
var text;
if (success) {
text = 'Shared';
} else {
text = 'Not shared';
}
console.warn(text)
})
but the share actions do not include either of google drive/onedrive/dropbox when I test on the device (screenshot available here).
Am I doing something wrong?
Looks like the device doesn't actually have google drive or dropbox installed. If those apps aren't installed, they won't be options for you to share to.