【问题标题】:How do I make a downloaded image show up in the Device's Gallery?如何使下载的图像显示在设备的图库中?
【发布时间】: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


【解决方案1】:

将它们保存到 SDCard 上的正确位置。 BlackBerry 设备将 SDCard 安装在 /SDCard,因此完整路径为 /SDCard/BlackBerry/pictures/

您还可以在图片目录中创建一个子目录,当从照片库应用程序查看时,该子目录将组织照片。

设备也有内置存储,但容量远小于大多数 SDCard。要将图片保存在那里,请使用路径/store/home/user/pictures/

【讨论】:

  • 感谢您的回答。这条路径是否适用于所有 BlackBerry 设备?你知道 Android 和 iOS 的路径吗?
  • 对于插入了工作 SDCard 的 BlackBerry 设备,此路径应该有效。 SDCards 是可选的,所以不是所有的设备都会有这个。 iOS 和 Windows Phone 没有向第三方应用程序公开的通用文件系统,因此它们的机制不同。我不确定 Android 是如何工作的。
  • 好吧,无论如何感谢@Micheal,Android 似乎可以接受我们提供的任何路径。任何关于在没有 SDCard 的情况下可以在 BlackBerry 上使用什么路径的线索?
  • 你会在这里找到答案:stackoverflow.com/questions/8258612/…
  • @EugenMartynov,谢谢,Micheal 提到的那些路径适用于 BlackBerry,我还需要 Android 和 iOS 的路径,你知道吗?
【解决方案2】:

当你关闭或停止你的应用程序时,你应该调用这个方法。这对我来说适用于 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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多