I need to create PDF-file (done) and save it to Files application, so user can access it anytime outside of my app. I tried rn-fetch-blob and react-native-fs packages and they worked fine for Android, but for iOS they can only create files to internal app storage (so files are not created in external storage, i.e. not in iOS Files application).
What options do I have to save my created files to Files application? I know that it is possible, i.g. Slack app allows saving files to Files app.
You have to either add the UISupportsDocumentBrowser key and set to true, or both the UIFileSharingEnabled and LSSupportsOpeningDocumentsInPlace keys in your Info.plist file
When the user will open a document from your app's Documents directory, through Files app, they would be editing the document in place. The changes are saved to your app's Documents directory.
Refer this for more.
you can try with react-native-fs to save and download in local directory.
var url = path
let fileName = path.split("/").pop()
// console.log('fileName', fileName)
var ext = this.extention(url);
ext = "." + ext[0];
const localFile = ${RNFS.LibraryDirectoryPath}/${fileName};
// console.log('localFile', localFile)
const options = {
fromUrl: path,
toFile: localFile,
fileCache: true
};
if (await RNFS.exists(localFile)) {
FileViewer.open(localFile, { displayName: this.props.route.params.conversationSubject });
} else {
await RNFS.downloadFile(options).promise
.then((res) => {
// console.log('res', res)
FileViewer.open(localFile);
})
.then((res) => {
// success
// console.log("success", res);
})
.catch(error => {
// error
console.log("Attachment open error: ", error);
});
}
I've added new functionality to react-native-share module to store a file only in Files app. It's not merged yet but you can use my fork version.
https://github.com/gevorg94/react-native-share/tree/share-files-app.
Replace "react-native-share": "X.X.X" with "react-native-share": "git+https://github.com/gevorg94/react-native-share.git#share-files-app",.
You can find usage in PR https://github.com/react-native-community/react-native-share/pull/681
Related
My google drive storage is running out of space. So, in order to free some space, I created a new google account and transferred the ownership of one of the largest folders in my original drive (~7 GB) to the new account's drive.
The problem is, that after 3 days of waiting, the folder is still consuming the storage of the original account's drive. I made sure that the new account is the owner of the folder, but the problem is still there.
Any ideas?
The problem is that changing the ownership of folder inside google drive does not change it for the subfolders and files, you have to change them separately or write an app script to search for file and subfolders owned by you and change the ownership.
Run the script below as your account, if you are using Google Workspace and you are the admin and need to do it to other accounts, then you need to get a service account first and run the code below.
Note:
This way makes a lot of noise with notifications on both sides, the old owner and the new one.
function ChangeFileOwnership(folder){
// New owner
var new_owner = '<Add new owner email>';
if (folder == null) {
var folderObj = DriveApp.getFolderById('<ID of parent folder>');
return ChangeFileOwnership(folderObj);
}
var fileIt = folder.searchFiles('"me" in owners');
while (fileIt.hasNext()) {
var file = fileIt.next();
Logger.log("Changing ownership of " + file.getName() + " to " + new_owner);
// Set the owner to be the new owner
try {
file.addEditor(new_owner);
}
catch(err){
Logger.log(err.message);
}
try {
file.setOwner(new_owner);
}
catch(err){
Logger.log(err.message);
}
}
// Get all the sub-folders and iterate
var folderIt = folder.getFolders();
while (folderIt.hasNext()) {
fs = ChangeFileOwnership(folderIt.next());
}
}
I have an NativeScript 6.8 Javascript app that downloads newer data files. I'm discovering that on iOS I cannot create files within the app folder. (At least, in release builds; in debug builds I can.) I can change my code to read data files from the Documents folder, but how can I pre-populate the Documents folder at build time with the original data files? I'd rather not copy all the data files at run time.
Or, have I misinterpreted the restriction that files cannot be created in the app folder (or subfolders) in iOS release builds?
Live-updating files on iOS is more involved than one might expect. So, yes, you need to access the live-updated files from the Documents folder, not back the files up to iCloud, and handle numerous timing conditions, such as the live-update running just before what would seem to be the initial copy of the file to the Documents folder (seems unlikely, but I've seen it happen while testing).
I've included the function I developed, below. For context, when I find a file online to be live-updated, I use an appSetting to save the file's date as a string (storing as a value loses precision).
The function isn't perfect, but for now, it gets the job done. I call this from app.js in a NativeScript 6.8 JavaScript project.
/**
* Copy files in app/files folder to Documents folder so they can be updated
*/
async function copyFilesToDocuments() {
let filesCopied = appSettings.getBoolean("copyFilesToDocuments", false);
if (!filesCopied) { // only copy files on first invocation
let filesFolder = fs.knownFolders.currentApp().getFolder("files"); // Folder object
let documentsFolder = fs.knownFolders.documents(); // Folder object
let fileEntities = await filesFolder.getEntities();
for (entity of fileEntities) {
let sourceFile = fs.File.fromPath(entity.path);
let targetFilePath = fs.path.join(documentsFolder.path, entity.name);
let targetDate = parseInt(appSettings.getString(entity.name, "0"), 10); // live-update date or 0
if (fs.Folder.exists(targetFilePath) && targetDate > global.dataDate ) { // if file has been live-updated
console.log("app.js copyFilesToDocuments: file '" + entity.name + "' skipped to avoid overwrite. ");
continue; // don't overwrite newer file
}
appSettings.remove(entity.name); // remove any live-update timestamp
let targetFile = fs.File.fromPath(targetFilePath);
let content = await sourceFile.read();
try {
await targetFile.write(content);
if (platform.isIOS) {
// Prevent file from being backed up to iCloud
// See https://stackoverflow.com/questions/58363089/using-nsurlisexcludedfrombackupkey-in-nativescript
// See https://stackoverflow.com/questions/26080120/cfurlcopyresourcepropertyforkey-failed-because-passed-url-no-scheme
NSURL.fileURLWithPath(targetFilePath).setResourceValueForKeyError(true, NSURLIsExcludedFromBackupKey);
}
// console.log("app.js copyFilesToDocuments file copied: " + entity.name);
} catch(e) {
console.warn("app.js copyFilesToDocuments error: " + e);
//TODO: app will fail at this point with some files not found :-(
} // end catch
} // end for
appSettings.setBoolean("copyFilesToDocuments", true);
} // end files not yet copied
} // end copyFilesToDocuments
I have used PDFTron to update/edit PDF files. I have followed the documentation for opening the PDF file which came from server, but I am not sure how to save the edited PDF file with this SDK (PDFTron).
I have referred below links to save PDF, but did not succeed.
https://www.pdftron.com/documentation/ios/guides/features/forms/export-data/
https://www.pdftron.com/api/ios/Enums/PTSaveOptions.html
I want to send XFDF file formats to server.
PDFTron saves PDF with annotation automatically after some time interval, but I want it to be saved by save button press. I am stuck on this saving process.
I have below code to import annotation and I don't know how to import this XFDF file and where do to get this XFDF file.
// Import annotations from XFDF to FDF
let fdf_doc: PTFDFDoc = PTFDFDoc.create(fromXFDF: xfdf_filename)
// Optionally read XFDF from a string
let fdf_doc: PTFDFDOc = PTFDFDoc.create(fromXFDF: xfdf_string)
// Merge FDF data into PDF doc
let doc: PTPDFDoc = PTPDFDoc(filepath: filename)
doc.fdfMerge(fdf_doc)
I don't want it to be customisations by myself, I just want it to be saved by me on pressing button.
Below is my query
How do I save the applied annotation on PDF by myself?
Once you've applied changes to the document data you'll probably want to do something with the updated PDF like letting the user download it or sending it back to your server.
If you just want to let the user download the edited file then no extra changes are necessary as pressing the download button will save the modified PDF to the user's computer.
To add a custom save button, here is a code sample.
If you want to get the modified PDF as an ArrayBuffer then you can use the
getFileData function on Document.
For example:
WebViewer(...)
.then(instance => {
const { documentViewer, annotationManager } = instance.Core;
documentViewer.addEventListener('documentLoaded', async () => {
const doc = documentViewer.getDocument();
const xfdfString = await annotationManager.exportAnnotations();
const options = { xfdfString };
const data = await doc.getFileData(options);
const arr = new Uint8Array(data);
const blob = new Blob([arr], { type: 'application/pdf' });
// upload blob to your server
});
});
I have followed the documentation for opening the PDF file which came from server
There are a few ways to do this - could you share which API you are using?
The main point of your question seems to be how to save the PDF via a button press (after you've merged in XFDF annotation data). Is this the case?
You can control where a remote document is shared by implementing the relevant delegate methods, likely specifically https://www.pdftron.com/api/ios/Protocols/PTDocumentControllerDelegate.html#/c:objc(pl)PTDocumentControllerDelegate(im)documentController:destinationURLForDocumentAtURL:
You can then save the document using this method:
https://www.pdftron.com/api/ios/Classes/PTDocumentBaseViewController.html#/c:objc(cs)PTDocumentBaseViewController(im)saveDocument:completionHandler:
I've been hitting my head on the screen for some time and just can't get it to work despite all the information I've found on git or stackoverflow.
What I'm trying to achieve:
Persistence of the images selected (from library or camera). Persistence also when I rebuild my app, which means when I run react-native run-ios for iOS, or react-native run-android for Android.
What I've implemented
Here is my function that is called when I add an image.
ImagePicker.showImagePicker({
storageOptions: {
path: 'myCustomPath',
waitUntilSaved: true,
cameraRoll: true,
skipBackup : true
},
noData: true
}, res => {
if (res.didCancel) {
console.log("User cancelled");
} else if (res.error) {
console.log("Error", res.error);
} else {
console.log("Picker Response:", res);
// Getting the fileName as for iOS the fileName is incorrect even with waitUntilSaved to true.
const path = res.uri.split("/");
const fileName = path[path.length-1];
let uri = "file://" + Platform.select({
android: RNFS.ExternalStorageDirectoryPath + "/Pictures/",
ios: RNFS.DocumentDirectoryPath+"/"
})
+ "myCustomPath/" + fileName
this.setState({
pickedImage: {
uri: uri,
}
});
this.props.onImagePicked({ uri: res.uri });
}
});
And in my home page, I display the image in <Thumbnail square style={{width:50, height:50}} source={info.item.image} />
---- EDIT START -----
---- SOLUTION ----
The solution was to reconstruct the path where I want to display the image using the fileName:
<Thumbnail square style={{width:50, height:50}} source={{uri : RNFS.DocumentDirectoryPath+"/"+info.item.image.fileName}} />
---- EDIT ENDS -----
The Result
So here are the results, you'll see works well on Android, but not on iOS.
I selected an image on the library using the react-native-image-picker.
Android
"Picker Response":
fileName: "image-d53839d1-fa89-4a61-b648-f74dace53f83.jpg"
fileSize: 235728
height: 1440
isVertical: true
originalRotation: 0
path: "/storage/emulated/0/Pictures/myCustomPath/image-d53839d1-fa89-4a61-b648-f74dace53f83.jpg"
type: "image/jpeg"
uri: "file:///storage/emulated/0/Pictures/myCustomPath/image-d53839d1-fa89-4a61-b648-f74dace53f83.jpg"
width: 1080
the save "uri" for my image is : file:///storage/emulated/0/Pictures/myCustomPath/image-d53839d1-fa89-4a61-b648-f74dace53f83.jpg
Behavior is as below :
left screenshot is after saving my image,
right screenshot is after a rebuild, opening the app, I still see my image!
--> Happy!
Now on iOS
"Picker Response" :
fileName: "IMG_0004.JPG"
fileSize: 470300
height: 1618
isVertical: true
latitude: 64.752895
longitude: -14.538611666666666
origURL: "assets-library://asset/asset.JPG?id=99D53A1F-FEEF-40E1-8BB3-7DD55A43C8B7&ext=JPG"
timestamp: "2012-08-08T21:29:49Z"
type: "image/jpeg"
uri: "file:///Users/[name]/Library/Developer/CoreSimulator/Devices/33076AD2-C989-47E9-A803-3E56CC4B09D6/data/Containers/Data/Application/4756F0A2-9CCC-4F9A-9315-D55434328FD9/Documents/myCustomPath/6A5C27E3-89F7-465F-A855-66749C92D086.jpg"
width: 1080
the save "uri" for my image is : file:///Users/[name]/Library/Developer/CoreSimulator/Devices/33076AD2-C989-47E9-A803-3E56CC4B09D6/data/Containers/Data/Application/4756F0A2-9CCC-4F9A-9315-D55434328FD9/Documents/myCustomPath/6A5C27E3-89F7-465F-A855-66749C92D086.jpg
Behavior is as below :
left screenshot is after saving my image,
right screenshot is after a rebuild, opening the app, I still see my image!
--> NOT HAPPY!
My issue
I've read all about the temporary aspect of the returned uri and see what react-native-image-picker says about it:
On iOS, don't assume that the absolute uri returned will persist.
I've found also this thread which had a similar issue, but none worked:
RNFS.DocumentDirectoryPath after a rebuild had a different UUID in the path, so the file was not found as it was saved with the previous UUID
I tried saving for iOS the uri as '~/Documents/myCustomPath/myfilename.jpg' had the same behavior. Displayed when added, blank after rebuild.
I had before the rebuild:
Document directory path being :
file:///Users/[user]/Library/Developer/CoreSimulator/Devices/33076AD2-C989-47E9-A803-3E56CC4B09D6/data/Containers/Data/Application/466CAF1A-AF8D-423C-9BF6-F0A242AF8038/Documents/
and my saved picture uri :
file:///Users/[user]/Library/Developer/CoreSimulator/Devices/33076AD2-C989-47E9-A803-3E56CC4B09D6/data/Containers/Data/Application/466CAF1A-AF8D-423C-9BF6-F0A242AF8038/Documents/myCustomPath/20969988-633B-46BD-8558-E39C3ADD6D12.jpg
but after the rebuild the Application UUID changed to BEE128C8-5FCF-483C-A829-8F7A0BB4E966 making now my document directory to be
file:///Users/[user]/Library/Developer/CoreSimulator/Devices/33076AD2-C989-47E9-A803-3E56CC4B09D6/data/Containers/Bundle/Application/BEE128C8-5FCF-483C-A829-8F7A0BB4E966/Documents
but still looking the picture in the uri :
file:///Users/[user]/Library/Developer/CoreSimulator/Devices/33076AD2-C989-47E9-A803-3E56CC4B09D6/data/Containers/Data/Application/466CAF1A-AF8D-423C-9BF6-F0A242AF8038/Documents/myCustomPath/20969988-633B-46BD-8558-E39C3ADD6D12.jpg
When the image is actually now located under:
file:///Users/[user]/Library/Developer/CoreSimulator/Devices/33076AD2-C989-47E9-A803-3E56CC4B09D6/data/Containers/Data/Application/BEE128C8-5FCF-483C-A829-8F7A0BB4E966/Documents/myCustomPath/20969988-633B-46BD-8558-E39C3ADD6D12.jpg
So how can I cope for that App UUID change after a rebuild as paths are dynamic on iOS.
Versions I use:
"react-native": "0.57.8",
"react-native-fs": "^2.13.2",
"react-native-image-picker": "^0.28.0"
As react-native-image-picker states
On iOS, don't assume that the absolute uri returned will persist.
This means that if you store the absolute uri to the file and then do react-native run-ios the application GUID will change and that absolute uri will no longer exist or work. However, and this is important the image will be in the folder that you saved it in, as it is copied across.
The only solution to fixing this is to save the directory and the filename of the file and reconstruct the uri when you want to use it.
So that means that you would save in state
this.setState({ image: '/myCustomPath/image.jpg' });
Then when you come to use it you would rebuild the path like this
let pathToFile = `${RNFS.DocumentDirectoryPath}${this.state.image}`
You could simply solve this (without rely on react-native-fs) by replacing the path before "/Documents/..." with a tilde "~".
let pathToFile = "file:///var/mobile/Containers/Data/Application/9793A9C3-C666-4A0E-B630-C94F02E32BE4/Documents/images/72706B9A-12DF-4196-A3BE-6F17C61CAD06.jpg"
if (Platform.OS === 'ios') {
pathToFile = '~' + pathToFile.substring(pathToFile.indexOf('/Documents'));
}
This is supported by React Native as you can see in their source code.
I've created a Cordova application that fetches images from a server and saves them to an iPad. However, when trying to display the images in the application, the images will not load. One example of such a file path could be:
file:///var/mobile/Containers/data/Application/FC87E925-9753-4D9F-AE27-54FCF9B0451E/Documents/-media-3405-company.png
However, when inspecting the cordova.file.applicationDirectory variable, I find another path, e.g. (note that the UUID differ even though I'm inspecting both variables in the same run)
file:///var/containers/Bundle/Application/D8266D08-18A4-4293-B78A-B4597FC0C6B8/salesApp.app/
Accordingly to the documentation, the correct path "should" be: (however, that does not work either)
file:///var/mobile/Applications/UUID/Documents/-media-3405-company.png
Here is the code I use to load the images, which are correctly saved to the device
const downloadFile = (url, fileName, callback) => {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, (fs) => {
fs.root.getFile(fileName, {
create: true,
exclusive: false
}, (fileEntry) => {
const fileURL = fileEntry.toURL()
const fileTransfer = new FileTransfer()
fileTransfer.download(
url,
fileURL,
(entry) => {
const file = entry.toURL() // <--- HERE
content.pushObject('Downloaded ' + entry + ' (' + fileName + ') ' + file)
callback(file)
},
(error) => {
content.pushObject('error ' + error.code + '(' + fileName + ')')
if (error.code === FileTransferError.CONNECTION_ERR) {
downloadFile(url, fileName) // Try again
} else {
decrement(url) // Ignore this file
}
}
)
}, (error) => {
alert(2)
})
}, () => {
alert(3)
})
}
Update: Inspecting the value for cordova.file.documentsDirectory, I found that it returns a path similar to: file:///var/mobile/Containers/Data/Application/{UUID}/Documents/.
Update: The following code will return two different UUIDs:
alert(cordova.file.applicationDirectory); // file:///var/containers/Bundle/Application/54E0F914-C45B-4B8F-9067-C13AF1967760/salesApp.app/
alert(cordova.file.documentsDirectory); // file:///var/mobile/Containers/Data/Application/73806E90-90B4-488C-A68A-2715C3627489/Documents/
When inspecting the path for entry.toURL() i get the same UUIDs as the one returned in cordova.file.documentsDirectory.
When you claim that "images will not load", then you should provide the code used to load the images.
The code you provided is to download the images and it works fine.
As you didn't provide the code used to load the images I have tried two things and both of them worked
Open the file on InAppBrowser. I installed the cordova-plugin-inappbrowser and opened the file like this window.open(file,'_blank');
Display the file on a img tag. I created a img tag in my index.html <img id="downloaded" src=""/> and on my callback I assign the file obtained to the src document.getElementById("downloaded").src = file;
Both of them worked.
So you should provide your code used to load the images because the problem might be there.
The path you are getting on the download is ok.
You are getting different UUIDs because the docs are outdated. Before Xcode 6/iOS8 the app sandbox had the Bundle container and the Data container inside the same folder (the one the docs mention with a common UUID), but since Xcode 6/iOS8 the app files (Bundle container) are in a path and the App data files are in another one (Data container).
But that shouldn't be a problem for you.
Answer that talks about the file structure changes