2017-03-17 92 views
0

任何人都可以請幫助我如何使用nativescript android應用程序下載遠程圖像到手機? 例如,Nativescript android如何將遠程圖像下載到設備?

<StackLayout> 
    <Image [src]="remoteImgUrl"></Image> 
    <Label tap="downloadImg({{remoteImgUrl}})" text="Download"> 
</StackLayout> 
當用戶點擊「下載」我需要保存圖像,在Android設備

+0

你可以使用Android的'nativescript-downloadmanager' - https://www.npmjs.com/package/nativescript-downloadmanager –

回答

1

這裏是在此基礎上應用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); // 
     }) 
+0

謝謝@Nick伊利耶夫 –