I will like to save an image from a web URL to the device can anyone help?
I've tried using the imageSource module but it only speaks of images from local device
-
You can write an API to return your image as base64string in json and have your app to persist the data using application settingsBaskar Rao– Baskar Rao2018-10-18 03:34:24 +00:00Commented Oct 18, 2018 at 3:34
-
Do you want to show that image in your component html ?Narendra– Narendra2018-10-18 04:38:15 +00:00Commented Oct 18, 2018 at 4:38
Add a comment
|
2 Answers
const imageSourceModule = require("tns-core-modules/image-source");
const fileSystemModule = require("tns-core-modules/file-system");
imageSourceModule.fromUrl(webURL).then((res) => {
const folderDest = fileSystemModule.knownFolders.currentApp();
const pathDest = fileSystemModule.path.join(folderDest.path, "test.png");
const saved = res.saveToFile(pathDest, "png");
if (saved) {
console.log("Image saved successfully!");
this.image = imageSourceModule.fromFile(pathDest);
}
thanks to @Narendra Mongiya for the first answer which help get the image from url
2 Comments
Narendra
Good to know that @Nsagha ! If you mark that as accepted answer and upvote, it may be helpful to others. Happy Coding !!
Nsagha Kingsly
Ok will do that
This is what your code should be (I am assuming that your webURL returns jpg/png etc)
const imageSource = require('image-source');
imageSource.fromUrl(webURL).then((res: any) => {
this.imageUrl = res;
}).catch(err => {
this.imageUrl = this.getIconSource('default_image');
});
and in html file
<Image [src]="imageUrl" loadMode="async"></Image>
and for the default images if URL return blank
public getIconSource(icon: string): string {
return isAndroid ? `res://${icon}` : 'res://images/' + icon;
}
1 Comment
Nsagha Kingsly
Thanks a lot, it gets the image but I do not know where or if the image is saving to the device please clarify me on this part thanks once more