使用 Cordova FileTransfer 下载的图像未显示在图库中

Image downloaded with Cordova FileTransfer is not showing up in Gallery

我正在使用 apache cordova 下载图像。但是图像没有出现在图库中。可能是什么问题以及如何解决?下面是我用来下载图片的代码。

var url = "http://example.com/some.jpg";
var filename = "varun.jpg";
var targetPath = cordova.file.externalRootDirectory + "Download/" + filename;
$cordovaFileTransfer.download(url, targetPath)
    .then(function (entry) {
        $scope.downloadProgress = 0;
    }, function (error) {
        alert(JSON.stringify(error));
    }, function (progress) {
        $timeout(function () {
            $scope.downloadProgress = (progress.loaded / progress.total) * 100;
        })
    });

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

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

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

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

如果这没有帮助,请查看这篇文章 http://docs.phonegap.com/en/edge/cordova_file_file.md.html#File

或这个Phonegap - Save image from url into device photo gallery

要在下载后在图库中显示图像,请添加此 cordova 插件:

ionic plugin add https://github.com/lotterfriends/refreshgallery

然后在您的代码中添加这一行:

var url = "http://example.com/some.jpg";
var filename = "varun.jpg";
var targetPath = cordova.file.externalRootDirectory + "Download/" + filename;
$cordovaFileTransfer.download(url, targetPath)
.then(function (entry) {
    $scope.downloadProgress = 0;
    refreshMedia.refresh(targetPath); // <--- this one here

}, function (error) {
    alert(JSON.stringify(error));
}, function (progress) {
    $timeout(function () {
        $scope.downloadProgress = (progress.loaded / progress.total) * 100;
    })
});

然而,这显示了图片库中的图像,而不是我的下载文件夹中的图像