這裏是在此基礎上應用here一個例子:
打字稿
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);
}
}
該方法接受的ImageSource作爲參數,從而可以用fromUrl方法稱它爲的ImageSource例如
import * as imageSource from "image-source";
imageSource.fromUrl(IMAGE_URL_HERE)
.then(res => {
saveFile(res); //
})
你可以使用Android的'nativescript-downloadmanager' - https://www.npmjs.com/package/nativescript-downloadmanager –