2

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

2
  • You can write an API to return your image as base64string in json and have your app to persist the data using application settings Commented Oct 18, 2018 at 3:34
  • Do you want to show that image in your component html ? Commented Oct 18, 2018 at 4:38

2 Answers 2

3
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

Sign up to request clarification or add additional context in comments.

2 Comments

Good to know that @Nsagha ! If you mark that as accepted answer and upvote, it may be helpful to others. Happy Coding !!
Ok will do that
0

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.