[英]Phonegap/Cordova - download jpg to local FS on Windows Phone 8/8.1
我一直在嘗試使用Windows Phone上的cordova 3.8從URL將圖像下載到本地文件系統。
我想存儲圖片並能夠在HTML視圖中使用它們。
我正在使用文件傳輸和文件系統插件:
我一直都在玩,但沒有成功。 我認為問題來自目標文件URI,因為我還沒有找到獲取下載文件夾路徑的任何方法。
我可以使用cordova-file插件文檔中的示例輕松地讀取和寫入文本文件,但是在如何獲取目錄路徑以將其傳遞給文件傳輸插件方面,我沒有找到任何東西。
任何想法 ? 我正在WP8上進行測試。
您可以制作自己的插件(如果需要,我可以解釋如何使用)並使用MediaLibrary.SavePicture方法。 將其放在您的插件類中:
public static byte[] ReadFully(Stream input)
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
return ms.ToArray();
}
}
public void SavePicture(string options)
{
string[] optString = getOptionStrings(options);
string url = optString[0];
string callbackId = optString[1];
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.BeginGetResponse(new AsyncCallback((result) =>
{
try
{
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result);
byte[] content = ReadFully(response.GetResponseStream());
MediaLibrary lib = new MediaLibrary();
lib.SavePicture("Test Picture", content);
DispatchCommandResult(new PluginResult(PluginResult.Status.OK), callbackId);
}
catch (Exception e)
{
DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, e), callbackId);
}
}), null);
}
並這樣稱呼它:
cordova.exec(function (result) {
// success
}, function (e) {
// error
}, 'YourPluginClass', 'SavePicture', [yourUrl]);
注意:您必須在清單中檢查ID_CAP_MEDIALIB_PHOTO功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.