Phonegap/Cordova - 在 Windows Phone 8/8.1 将 jpg 下载到本地 FS
Phonegap/Cordova - download jpg to local FS on Windows Phone 8/8.1
我一直在尝试在 Windows Phone.
上使用 cordova 3.8 从 url 下载图像到本地文件系统
我想存储图片并能够在 HTML 视图中使用它们。
我正在使用文件传输和文件系统插件 :
- https://github.com/apache/cordova-plugin-file-transfer
- http://docs.phonegap.com/en/edge/cordova_file_file.md.html
我一直在尝试两者,但没有成功。我认为问题来自目标文件 URI,因为我还没有找到任何获取下载文件夹路径的方法。
我可以使用 cordova-file 插件文档中的示例轻松读取和写入文本文件,但我没有找到任何有关如何获取目录路径以便将其传递给文件传输插件的信息。
有什么想法吗?我正在 WP8 上测试。
您可以制作自己的插件(如果需要我可以解释如何)并使用 MediaLibrary.SavePicture 方法。将其放入您的插件 class:
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 功能。
我一直在尝试在 Windows Phone.
上使用 cordova 3.8 从 url 下载图像到本地文件系统我想存储图片并能够在 HTML 视图中使用它们。
我正在使用文件传输和文件系统插件 :
- https://github.com/apache/cordova-plugin-file-transfer
- http://docs.phonegap.com/en/edge/cordova_file_file.md.html
我一直在尝试两者,但没有成功。我认为问题来自目标文件 URI,因为我还没有找到任何获取下载文件夹路径的方法。
我可以使用 cordova-file 插件文档中的示例轻松读取和写入文本文件,但我没有找到任何有关如何获取目录路径以便将其传递给文件传输插件的信息。
有什么想法吗?我正在 WP8 上测试。
您可以制作自己的插件(如果需要我可以解释如何)并使用 MediaLibrary.SavePicture 方法。将其放入您的插件 class:
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 功能。