Catalin Cardei
Catalin Cardei

Reputation: 324

$cordovaFile.readAsArrayBuffer return error with Android Device

I am trying to store an image to Firebase Storage and I am facing some difficult doing it with Android device. It's working fine with iOS.

The problem is that I get the following error when I select the picture from library:

FileError {code: 1, message: "NOT_FOUND_ERR"}

Here is my code:

$scope.fireStoreAvatar = function($sourceType) {

  if ($sourceType == 1) {

   var options = {
        quality: 100,
        destinationType: Camera.DestinationType.FILE_URI,
        sourceType: Camera.PictureSourceType.CAMERA,
        allowEdit: true,
        encodingType: Camera.EncodingType.JPEG,
        targetWidth: 1024,
        targetHeight: 1024,
        popoverOptions: CameraPopoverOptions,
        saveToPhotoAlbum: false,
        correctOrientation:true
      };

  } else {

    var options = {
          quality: 100,
          destinationType: Camera.DestinationType.FILE_URI,
          sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
          allowEdit: true,
          encodingType: Camera.EncodingType.JPEG,
          targetWidth: 1024,
          targetHeight: 1024,
          popoverOptions: CameraPopoverOptions,
          saveToPhotoAlbum: false,
          correctOrientation:true
        };

  }
        // Get image from camera or library
        $cordovaCamera.getPicture(options).then(function(imageData) {


          var fileName = "";
          var path1 = "";
          var path2 = "";

          if ($ionicPlatform.is("android")) {

              $log.debug('android:');

              var path1     = cordova.file.cacheDirectory;
              var fileName  = imageData.replace(/^.*[\\\/]/,'');
              var path2     = imageData.replace(fileName,'');

          } else {

              $log.debug('ios:');

              path = cordova.file.tempDirectory;
              var fileName = imageData.replace(/^.*[\\\/]/,'');

          }

          $log.debug('$cordovaCamera.getPicture      => imageData:  ' + imageData);
          $log.debug('cordova.file.cacheDirectory    => path1:      ' + path1);
          $log.debug('imageData.replace(fileName,"") => path2:      ' + path2);
          $log.debug('                                  fileName:   ' + fileName);

          $cordovaFile.checkFile(path1, fileName)
           .then(function (data) {

              $log.info('checkFile(path1, fileName) => OK');
              $log.info(data);

           }, function (error) {

             $log.warn('checkFile(path1, fileName) => NOT OK');
             $log.error(error);

           });

           $cordovaFile.checkFile(path2, fileName)
            .then(function (data) {

               $log.info('checkFile(path2, fileName) => OK');
               $log.info(data);

            }, function (error) {

              $log.warn('checkFile(path2, fileName) => NOT OK');
              $log.error(error);

            });

          $cordovaFile.readAsArrayBuffer(path1, fileName)
          .then(function (data) {

            $log.info('readAsArrayBuffer(path1, fileName) => OK');
            $log.info(data);

            // success
          }, function (error) {
            // error
            $log.warn('readAsArrayBuffer(path1, fileName) => NOT OK');
            $log.error(error);

          });

          $cordovaFile.readAsArrayBuffer(path2, fileName)
          .then(function (data) {

            $log.info('readAsArrayBuffer(path2, fileName) => OK');
            $log.info(data);

            // success
          }, function (error) {
            // error
            $log.warn('readAsArrayBuffer(path2, fileName) => NOT OK');
            $log.error(error);

          });


      }, function(err) {
        // error
        $log.error("Upload error: " + err);
      });

}

I tried with another path (path2) and works if I take a picture with the device camera but not works if i select a picture from gallery.

Selecting picture from Device Gallery:

enter image description here

Taking picture with Device Camera:

enter image description here

Well, you can observe that using path 2 and device camera, the $cordovaFile.readAsArrayBuffer works fine.

Maybe someone can give me some tips. Thanks!

--

I was trying to make this following Aaron Saunders tutorials on youtube:

Upvotes: 0

Views: 869

Answers (2)

Jijo Cleetus
Jijo Cleetus

Reputation: 2867

I was also tried the youtube example and got the same error. I have fixed this issue by using a workaround. Please find the details below.

Below code was throwing the error

$cordovaFile.readAsArrayBuffer(cordova.file.tempDirectory, fileName)

Actually readAsArrayBuffer is expecting 2 parameters path and file. So we can use a workaround is like, replace the cordova.file.tempDirectory with the actual file path before you do

data.replace(/^.*[\\\/]/, '');

Example: if the value of data is file:///storage/sdcard0/Android/data/com.yourdomain/cache/1533902301139.jpg Then use the below code

$cordovaFile.readAsArrayBuffer('file:///storage/sdcard0/Android/data/com.yourdomain/cache', fileName)

It worked for me with one line code change.

Upvotes: 0

Aaron Saunders
Aaron Saunders

Reputation: 33345

take another look at my code, there seems to be some functions missing from your implementation.

window.resolveLocalFileSystemURL(_imagePath, (fileEntry) => {})

https://github.com/aaronksaunders/firebaseStorage2/blob/master/src/pages/home/home.ts#L77

Upvotes: 1

Related Questions