如何使下载的图像显示在设备的图库中?

时间:2012-08-08 06:37:51

标签: android ios blackberry cordova

我有一个phonegap照片库应用程序,可以选择将图像下载到设备。

目前,我可以将图像保存到设备的SD卡中,但它们不会显示在设备的照片库/图库应用程序中。

以下是我正在使用的代码:

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/
  • 我无法在黑莓手机上进行测试,但我已将其设置为首先尝试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);
    });
};

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:1)

当你关闭或停止你的应用程序时,你应该调用这个方法。这对我和4.4以及较低的android sdk版本都有用。

import 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();
}