Upon selecting an Image with the Image Picker I receive a content// url
content://com.android.providers.media.documents/document/image%3A139
When using ImageSource.fromAsset() I am returned in empty object. My current goal is to save that image as a new image so I can send it in a req.
I have also tried Utils.android.getApplicationContext().getContentResolver() with no avail.
public onSelectSingleTap() {
return new Promise((resolve, reject) => {
this.isSingleMode = true;
let context = imagepicker.create({
mode: "single",
mediaType: ImagePickerMediaType.Image,
});
context
.authorize()
.then(() => {
return context.present();
})
.then((selection) => {
if (selection.length > 0) {
ImageSource.fromAsset(selection[0])
.then((imageSource) => {
console.log('Image Source: ', imageSource)
let documentsFolder = knownFolders.documents();
let random = Math.floor(Math.random() * 1000000000);
const filePath: string = path.join(documentsFolder.path, 'image' + random.toString());
let saved = imageSource.saveToFile(filePath, 'jpg')
if (saved) {
const savedFilePath: string = path.join(documentsFolder.path, 'image' + random.toString() + '.jpg');
const file: File = File.fromPath(savedFilePath);
// resolve({file: file, content: selection[0]})
}
})
.catch((err) => console.error("Error loading ImageSource:", err));
}
});
})
}
ImageSource: {"android": {}}
I am not sure if I need to SAVE as a new image or get the actual file path to send it in my request.
Most other answers that I come across are deprecated.
I've had to do this in several places in an app I built a few years ago. That app is using @nativescript/[email protected] and @nativescript/[email protected]. I'm thinking it should still work in current versions.
I've implemented a reusable service for triggering the image picker and returning an array of image paths. The array is typed as an array of strings, but I'm not sure what it actually is. (not typing things properly was a huge oversight back then)
Once you have your list of image paths, you can send them to a backend via the background http service: