kandan
kandan

Reputation: 737

Load image from project file into ImageSource with Nativescript

Have a local folder with images (app/resources/mock/images). This folder contains images which are used when the app is generated to read mock services.

Need to load an image from that folder into an ImageSource file, which then I send to a mocking method which downloads to the file disk (saveToFile) so it can be viewed. But have not found a way to do that.

ImageSoure.loadFromFile: Will load image from already saved images in file system (not finding them: FileNotFoundException)

ImageSource.fromResource: Requires to put images in the resources folder which is then included in the APK

This is not loading the image:

let image = new ImageSource();
let result = image.loadFromFile("../resources/mock/images/test-image.jpg");

How could I do it of is there any other better way to deal with this?

Thanks

Upvotes: 0

Views: 2107

Answers (2)

Ian MacDonald
Ian MacDonald

Reputation: 14030

I'm not sure about your usage of loadFromFile. I have used fromFile in the past:

import { fromFile, ImageSource } from 'tns-core-modules/image-source/image-source';

let image: ImageSource = fromFile('~/resources/mock/images/test-image.jpg');

Upvotes: 1

Manoj
Manoj

Reputation: 21908

You can't use relative path for images. Depending on your project structure, "~/resources/mock/images/test-image.jpg" or "~/app/resources/mock/images/test-image.jpg" should work.

Upvotes: 1

Related Questions