【发布时间】:2012-08-08 06:37:51
【问题描述】:
我有一个 phonegap 照片画廊应用程序,可以选择将图像下载到设备。
目前,我可以将图像保存到设备的 SDCard 中,但它们不会显示在设备的照片库/图库应用程序中。
这是我正在使用的代码:
var remoteFile = encodeURI($("#imageView_content").find("img").attr("src"));
var localFileName = remoteFile.substring(remoteFile.lastIndexOf('/') + 1);
var downloadSuccess = function() {
alert("Download sucessful!\nFile Saved at: " + localFileName);
};
var handleDownloadedFile = function(entry) {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, downloadSuccess, fail);
};
var startFileTransfer = function(fileEntry) {
var localPath = fileEntry.fullPath;
var ft = new FileTransfer();
ft.download(remoteFile, localPath, handleDownloadedFile, fail);
};
var createLocalFile = function(dir) {
dir.getFile(localFileName, {
create : true,
exclusive : false
}, startFileTransfer, fail);
};
var createPhotosDir = function(fileSys) {
fileSys.root.getDirectory("myAppName", {
create : true,
exclusive : false
}, createLocalFile, fail);
};
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, createPhotosDir, fail);
我正在使用https://build.phonegap.com 构建应用程序
更新:
我正在尝试使用取决于设备的路径,但这似乎不起作用:
- 在 iOS(模拟器)上,调用失败 - 路径:
file:///private/var/root/Media/DCIM/100APPLE/myApp/ - 在 Android 上,下载成功,但在设备重新启动之前,图片仍然不会显示在图库中 - 路径:
dcim/myApp/ - 我无法在 BlackBerry 上进行测试,但我已将其设置为先尝试
file:///SDCard/BlackBerry/pictures/myApp/,然后在失败时尝试file:///store/home/user/pictures/myApp/
这是设备检测代码:
var platform = (device.platform || navigator.userAgent).toLowerCase();
if (platform.match(/iphone/i) || platform.match(/ipad/i) || platform.match(/ios/i)) {
window.resolveLocalFileSystemURI("file:///private/var/root/Media/DCIM/100APPLE/", createPhotosDir_fromDir, callPhotosDir);
} else if (platform.match(/blackberry/i) || navigator.userAgent.toLowerCase().match(/blackberry/i)) {
window.resolveLocalFileSystemURI("file:///SDCard/BlackBerry/pictures/", createPhotosDir_BB, callPhotosDir);
} else if (platform.match(/android/i)) {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, createPhotosDir_Droid, callPhotosDir);
} else {
callPhotosDir();
}
这里是目录选择函数:
var callPhotosDir = function() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, createPhotosDir, fail);
};
var createPhotosDir_fromDir = function(dir) {
dir.getDirectory("myApp", {
create : true,
exclusive : false
}, createLocalFile, callPhotosDir);
};
var createPhotosDir_Droid = function(fileSys) {
fileSys.root.getDirectory("dcim/myApp", {
create : true,
exclusive : false
}, createLocalFile, callPhotosDir);
};
var createPhotosDir_BB = function(dir) {
dir.getDirectory("myApp", {
create : true,
exclusive : false
}, createLocalFile, function() {
window.resolveLocalFileSystemURI("file:///store/home/user/pictures/", createPhotosDir_fromDir, callPhotosDir);
});
};
【问题讨论】:
-
你的图片的绝对路径是什么?如果您将它们保存为“image.jpg”而不是“image”,它们应该会出现在图库中。
-
感谢您的评论。是的,下载的图像确实具有有效的扩展名,例如
.jpeg或.jpg或.png。图片的名称是通过它在localFileName中的绝对 URL 获得的(参见代码示例的前两行)。 -
然后它应该出现在您的画廊中,除非您没有将文件设置为不可见。但正如我所见,你没有。所以尝试重新启动你的手机,因为媒体浏览器需要找到它们,但如果你不更改目录中的任何内容,则不会触发。
-
在我的 Android 上,它会显示在我的文件资源管理器
/sdcard/myAppName/image.jpg,但不会显示在图库中,即使在重新启动手机后也是如此。或许有一个特定的路径可以让 Gallery 应用在每个 Android 设备上扫描图像? -
我认为先生。 Sathvik,您应该使用 sendBroadcast 方法在图库中显示您的图像。但此方法仅适用于
标签: android ios blackberry cordova