Android 如何使下载的图像显示在设备中';s画廊?
我有一个phonegap照片库应用程序,可以选择将图像下载到设备上 目前,我可以将图像保存到设备的SD卡中,但它们不会显示在设备的照片库/图库应用程序中 以下是我正在使用的代码:Android 如何使下载的图像显示在设备中';s画廊?,android,ios,blackberry,cordova,Android,Ios,Blackberry,Cordova,我有一个phonegap照片库应用程序,可以选择将图像下载到设备上 目前,我可以将图像保存到设备的SD卡中,但它们不会显示在设备的照片库/图库应用程序中 以下是我正在使用的代码: var remoteFile = encodeURI($("#imageView_content").find("img").attr("src")); var localFileName = remoteFile.substring(remoteFile.lastIndexOf('/') + 1); var dow
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);
我正在使用它来构建应用程序
更新:
我正在尝试根据设备使用路径,但这似乎不起作用:
- 在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);
});
};
将它们保存到SD卡上的正确位置。BlackBerry设备在/SDCard上安装SD卡,因此完整路径为
/SDCard/BlackBerry/pictures/
您还可以在图片目录中创建一个子目录,当从照片库应用程序查看照片时,该目录将组织照片
设备也有内置存储,尽管容量比大多数SD卡小得多。要在那里保存图片,请使用路径
/store/home/user/pictures/
当您关闭或停止应用程序时,应调用此方法。这对我来说适用于4.4版和更低版本的android sdk
导入android.media.MediaScannerConnection
public void addImageIntoGallery() {
String version = Build.VERSION.RELEASE;
if(! version.contains("4.4")){
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_MOUNTED);
String mCurrentPhotoPath = "file://" + Environment.getExternalStorageDirectory()+"/myDirectory";
File file = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
}else{
MediaScannerConnection.scanFile(this, new String[] { Environment.getExternalStorageDirectory().toString() }, null, new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri)
{
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
Log.i(TAG, "Scanned ................" + path);
}
});
}
}
@Override
protected void onPause() {
addImageIntoGallery();
super.onPause();
}
你的图像的绝对路径是什么?如果你将它们保存为“image.jpg”而不是“image”,它们应该会出现在gallery上。谢谢你的评论。是的,下载的图像确实有有效的扩展名,如
.jpeg
或.jpg
或.png
。图像的名称是通过localFileName
中的绝对URL获得的(请参见代码示例的前两行)。然后,除非您没有将文件设置为不可见,否则它应该显示在您的库中。但正如我看到的,你没有。因此,请尝试重新启动手机,因为媒体浏览器需要找到它们,但如果您不更改目录中任何不会被触发的内容。在我的Android上,它会显示在我的文件资源管理器中的/sdcard/myAppName/image.jpg
,但即使重新启动手机,也不会显示在多媒体资料中。也许有一个特定的路径可以让Gallery应用程序在每个Android上扫描图像?我想Sathvik先生,你应该使用sendBroadcast方法在Gallery中显示你的图像。但此方法仅适用于<4.4.0谢谢您的回答。这条路径在所有黑莓设备中都很常见吗?你知道安卓和iOS的路径吗?对于插入SD卡的黑莓设备,该路径应该有效。SD卡是可选的,因此并非所有设备都具有此功能。iOS和Windows Phone没有向第三方应用公开的通用文件系统,因此它们的机制不同。我不确定安卓系统是如何工作的。无论如何,谢谢@Micheal,安卓系统似乎接受我们给出的任何路径。关于在没有SD卡的情况下什么路径可以在BlackBerry上运行有任何线索吗?你可以在这里找到答案:@EugenMartynov,谢谢你,Micheal提到的那些路径可以在BlackBerry上运行,我还需要Android和iOS的路径,你知道吗?