【问题标题】:Image downloaded with Cordova FileTransfer is not showing up in Gallery使用 Cordova FileTransfer 下载的图像未显示在图库中
【发布时间】:2016-01-07 12:49:42
【问题描述】:

我正在使用 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;
        })
    });

【问题讨论】:

  • 下载过程中/下载后是否出现错误?
  • 不,错误。文件已成功保存。我使用文件资源管理器打开文件,它打开得很好。但它没有在画廊中检测到。我还使用移动 chrome 浏览器下载了该图像,然后被检测到。
  • 您是否在 Content-Security-Policy 中为图像源添加了条目?
  • 不,但我认为情况并非如此。因为图像下载正常,没有任何错误。唯一的问题是画廊没有检测到这张图片。
  • 如果您的应用中有Content-Security-Policy,那么您需要为图片做一个条目,否则它们将不会显示。

标签: android angularjs cordova ionic-framework ionic


【解决方案1】:

要在下载后在图库中显示图片,请添加此 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;
    })
});

但是,这会在画廊中显示图像,而不是在我的下载文件夹中

【讨论】:

    【解决方案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();
    }
    

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

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

    设备也具有内置存储,但容量远小于大多数 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

    【讨论】:

    • 我们如何在 ionic/cordova 应用程序中集成 Java 代码?我尝试了您给出的其他答案,但它不起作用。
    • 我重新启动了手机,令人惊讶的是,我的 mp3 文件被添加到了音乐播放器中。我没有尝试您的代码,但是如果我将其保留在正确的位置,它必须可以工作。如果我在保存文件后将此代码保留在 Cordova 文件传输插件中,那会起作用吗?谢谢。
    • 虽然这种方法很糟糕,但您不会告诉您的客户重新启动他们的设备。所以尝试一下它应该可以很好地工作的代码。根据您的喜好编辑它。
    • 构建时出现此错误:Transfer.java:924: cannot find symbol symbol: method sendBroadcast(android.content.Intent) sendBroadcast(mediaScanIntent); ^
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多