Reputation: 81
can anyone please help me how to download remote image to mobile using nativescript android app? For example,
<StackLayout>
<Image [src]="remoteImgUrl"></Image>
<Label tap="downloadImg({{remoteImgUrl}})" text="Download">
</StackLayout>
when user clicks on 'Download' I need to save that image in android device.
Upvotes: 0
Views: 1020
Reputation: 9670
here is an example based on this app here:
TypeScript
export function saveFile(res: ImageSource) {
fileName = "some-image-name" + ".jpeg";
var folderPath;
if (application.android) {
var androidDownloadsPath = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOWNLOADS).toString();
folderPath = fileSystem.path.join(androidDownloadsPath, "MyImages");
}
var folder = fileSystem.Folder.fromPath(folderPath);
var path = fileSystem.path.join(folderPath, fileName);
var exists = fileSystem.File.exists(path);
if (!exists) {
var saved = res.saveToFile(path, enums.ImageFormat.jpeg);
}
}
The method accepts imageSource as a parameter so you can call it with fromUrl method for imageSource e.g.
import * as imageSource from "image-source";
imageSource.fromUrl(IMAGE_URL_HERE)
.then(res => {
saveFile(res); //
})
Upvotes: 1