10 Replies Latest reply on Sep 12, 2016 4:32 AM by raresl88271813

    cannot write image blob data to file

    raresl88271813 Level 1

      Hi

       

      I am trying to write in persistent storage the blob data of an image ... although i do not get an error nothing gets written

      window.rootFS.getFile(window.mainDirectory + "/" + $scope.sCurrentDate + "/" + fileName.substring(0, fileName.lastIndexOf('.')) + ".jpg", {create:true}, function(file) {

        file.createWriter(function(fileWriter) {

        fileWriter.write(oXHR.responseText);
         fileWriter.onwriteend = function (e) {

             console.log("image file => ");
             console.log(e);
             console.log(file);
         });

         };
        }, function(error) {

        console.log(error);
        });
      }, function(e) {

        console.log(e);
      });

       

      i honestly do not know what to try anymore

       

       

        • 1. Re: cannot write image blob data to file
          VectorP Level 4

          I can't even see what you tried. :-(

           

          Your first line starts with

          window.rootFS.getFile(window.mainDirectory + "/" + $scope.sCurrentDate + "/" + fileName.substring(0, fileName.lastIndexOf('.')) + ".jpg"

           

          Now, did you debug this?

          - Is that line of code ever reached at all?, If so:

          - what is the object type value of 'rootFS' at that point?

          - is method 'getFile' defined at that point?

          - what is the value of mainDirectory, of $scope and of $scope.sCurrentDate?

          - what is the value of fileName

           

          And furthermore, it would be interesting to know:

          a. which plugin and which version did you include?

          b. did you include a reference to cordova.js in your html (without including such file in your assets)?

          c. do you wait for the deviceready event to fire before even attempting to write this file?

          • 2. Re: cannot write image blob data to file
            raresl88271813 Level 1

            so the entire part of the code works because the console.logs are executed .. if it helps and cand post that output

            the pluing i am using is this =>

            <gap:plugin name="cordova-plugin-file" source="pgb" spec="3.0.0" />
            <gap:plugin name="cordova-plugin-file-transfer" source="pgb" spec="1.3.0" />

             

            so the file gets created but the image link appears broken

            i thought that maybe i am writing the blob wrong

            • 3. Re: cannot write image blob data to file
              raresl88271813 Level 1

              should i be writing the blob directly or writer.write(new Blob([data.responseText], {type: 'image/jpeg'});?

              • 4. Re: cannot write image blob data to file
                VectorP Level 4

                Good. So, something changed meanwhile, because in your OP, you wrote:

                nothing gets written

                and now you say:

                the file gets created but the image link appears broken

                 

                About the plugins:

                1. the File plugin is integrated in the File-Transfer plugin, so you would not need the former.

                2. there are no such plugins by those names at the old, deprecated 'pgb' repo

                3. the latest versions of these plugins at npm are 4.2.0  and 1.5.1, so you included quite old versions

                4. you should use the latest syntax and npm, so write:

                <plugin name="cordova-plugin-file-transfer" />

                 

                Now, what is 'the image link' and how does it appear to be broken?

                • 5. Re: cannot write image blob data to file
                  VectorP Level 4

                  should i be writing the blob directly or writer.write(...)

                   

                  ??

                  In the code from your original question, you don't have a variable/object 'writer', so you can't call 'writer.write()'.

                  • 6. Re: cannot write image blob data to file
                    raresl88271813 Level 1

                    my pb is i cannot check what gets written because until now i could not access the file on the phone or the pc ... so

                    1) i could not access directly (in phone or over the pc) the path that gets returned

                    2) the path exists because gapdebug does not return file not found ... just image broken

                     

                    i will update the config.xml and try again

                     

                    the code i was talking about is this:

                    fileWriter.write(oXHR.responseText);

                    in plain js should be like this ... in phonegap ... who knows

                    fileWriter.write(new Blob([oXHR.responseText], {type: 'image/jpeg'}));

                    • 7. Re: cannot write image blob data to file
                      VectorP Level 4

                      Phonegap is just the building tool.

                      You probably meant to say that you don't know what the code should be for the various OS and devices.

                       

                      Well, on the devices, the target destination directory will be different for each OS. But the javascript code will be identical. Use whatever you consider 'plain js'.

                       

                      You still didn't say what URL is generated from your code and which platform and version you are testing this on, so it's hard to check whether it's correct for the given situation. You should, however, use the generic directory constants as documented in the File Plugin's docs. From your code, it's unclear whether or not you use these for the variable mainDirectory

                      • 8. Re: cannot write image blob data to file
                        raresl88271813 Level 1

                        i updated config.xml and retried

                         

                        "file:///data/user/0/com.wowzaaa.rts/files/files/housetube-ricoh-theta-s/2016-9-1/360_0089 .jpg"

                         

                        this is the value of nativeURL

                        how can i find it on the phone if browsing using the PC ... i tried looking here but it is empty

                        This PC\Galaxy A5 (2016)\Phone\Android\data\com.wowzaaa.rts\files

                        • 9. Re: cannot write image blob data to file
                          VectorP Level 4

                          Sorry, but it is really very hard to follow what you are saying, because with every post, suddenly new variable names come up.

                          What is this variable 'nativeURL'? Where is it used? And which directory constant from the File Plugin's docs do you use to:

                          a. write the file

                          b. display the file?

                           

                          Btw: I am not sure how you validate that your blob data are really of .jpg type. Is that done locally or server side?

                          • 10. Re: cannot write image blob data to file
                            raresl88271813 Level 1

                            ok so this code:

                            window.rootFS.getFile(window.mainDirectory + "/" + $scope.sCurrentDate + "/" + fileName.substring(0,fileName.lastIndexOf('.')) + ".jpg", {create:true}, function(file) {});

                             

                            has in the success function the file object ... that object has a prop called nativeUrl ... which has a different value depending on the os (ios or android)

                            here is the output of the "file" variable from the success function ... check the nativeURL prop

                            https://postimg.org/image/n54zataeb/