0

I am downloading an Image using apache cordova. But the image is not showing up in the gallery. What could be the problem and how to fix? Below is my code using to download the image.

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;
        })
    });
Varun Kakumani
  • 117
  • 1
  • 18
  • Are there any errors produced during/after download? – drys Jan 07 '16 at 13:00
  • 1
    No, errors. File got saved successfully. I opened file using file explorer and it opened fine. But it is not detecting in gallery. Also I downloaded that image using mobile chrome browser and it got detected then. – Varun Kakumani Jan 07 '16 at 13:05
  • Have you made an entry in your Content-Security-Policy for the image source? – Joerg Jan 07 '16 at 14:01
  • 1
    No, but I don't think that's the case. Because image is downloading fine without any errors. Only thing is that gallery is not detecting this image. – Varun Kakumani Jan 07 '16 at 14:03
  • If you have a Content-Security-Policy in your app, then you need to make an entry for images, otherwise they will not be shown. – Joerg Jan 07 '16 at 15:42

2 Answers2

2

To show the image on the gallery after the download add this cordova plugin:

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

and after that just add this line on your code:

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

However this shows the image on gallery, but not on Download folder for me

Creos Web
  • 21
  • 2
1

When you close or stop your app,you should call this method.This is working for me for both 4.4 as well as lower android sdk version.

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

Save them into the right place on the SDCard. BlackBerry devices mount the SDCard at /SDCard, so the full path would be /SDCard/BlackBerry/pictures/

You can also create a subdirectory in the pictures directory, which will organize the photos when viewed from the photo gallery app.

Devices have built-in storage as well, though the capacity is substantially smaller than most SDCards. To save pictures there, use the path /store/home/user/pictures/

if this does not help then view this article http://docs.phonegap.com/en/edge/cordova_file_file.md.html#File

or this Phonegap - Save image from url into device photo gallery

Community
  • 1
  • 1
youngdero
  • 382
  • 2
  • 16
  • 1
    How can we integrate Java code in ionic/cordova app? I tried the other answer you gave, but it is not working. – Varun Kakumani Jan 07 '16 at 13:20
  • 1
    http://trinhtrunganh.com/correct-way-to-integrate-cordova-plugin-in-angularjs-ionic-framework/ or https://forum.ionicframework.com/t/calling-native-code-sdks-from-ionic/12748 – youngdero Jan 07 '16 at 13:27
  • 1
    I restarted my phone and surprisingly my mp3 files are added to music player. I didn't try your code, but it must work if I keep it correct place. If I keep this code in Cordova file transfer plugin just after file is saved, will that work? Thanks. – Varun Kakumani Jan 08 '16 at 03:04
  • While that method is bad is that you won't tell your client to restart their device.so try the code it should work very well.edit it to your taste. – youngdero Jan 08 '16 at 05:25
  • 1
    I am getting this error while build: `Transfer.java:924: cannot find symbol symbol: method sendBroadcast(android.content.Intent) sendBroadcast(mediaScanIntent); ^` – Varun Kakumani Jan 08 '16 at 05:35
  • i dont know what the problem may be but check http://hmkcode.com/android-sending-receiving-custom-broadcasts/ ............ http://www.techotopia.com/index.php/Broadcast_Intents_and_Broadcast_Receivers_in_Android_Studio and https://code.google.com/p/csipsimple/source/browse/trunk/CSipSimple/src/com/csipsimple/service/SipService.java?r=356 – youngdero Jan 08 '16 at 06:55