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

时间:2016-01-07 12:49:43

标签: android angularjs cordova ionic-framework ionic

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

2 个答案:

答案 0 :(得分:2)

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

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

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

OR

将它们保存到SD卡上的正确位置。 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