Zi1989 opened a new issue #373: Cannot save image into the phone
URL: https://github.com/apache/cordova-plugin-file/issues/373
 
 
   <!--
   Please have a look at the issue templates you get when you click "New issue" 
in the GitHub UI.
   We very much prefer issues created by using one of these templates.
   -->
   
   ### Issue Type
   <!-- Please check the boxes by putting an x in the [ ] like so: [x] -->
   
   - [ ] Bug Report
   - [ ] Feature Request
   - [ x] Support Question
   
   ## Description
   
   ## Information
   I am trying to build an android app using cordova, The app requires the user 
to take photo and save it in their phone when there is no internet and upload 
it later whenever there is internet connection.
   The problem is I am not able to save the photos into the phone(android only).
   ### Command or Code
   //fileBlob is the file created after taking the photo
   PageModule.prototype.storeImage = function(fileBlob) {
   window.requestFileSystem(window.PERSISTENT,10 * 1024 * 1024, onInitFs, 
onErrorLoadFs);
   function onErrorLoadFs() {
       console.log("Create file fail...");
       }
   function onInitFs(fs) {
       console.log('file system open: ' + fs.name);
       gotFileSystemButNowCheckForWritePermission(fs.root);
       saveFile(fs.root, fileBlob, "downloadedImage.png");
   
   }
   };
   function gotFileSystemButNowCheckForWritePermission(fs)
   {
       cordova.plugins.diagnostic.requestRuntimePermission(
           function(status){
               switch(status){
                   case cordova.plugins.diagnostic.permissionStatus.GRANTED:
                       console.log("Permission granted");
                       saveFile(fs);
                       break;
                   case cordova.plugins.diagnostic.permissionStatus.DENIED:
                       console.log("Permission denied");
                       alert('App needs access to the phones file storage in 
order to run');
                       gotFileSystemButNowCheckForWritePermission(fs);
                       break;
                   case 
cordova.plugins.diagnostic.permissionStatus.DENIED_ALWAYS:
                       console.log("Permission permanently denied");
                       alert('Cannot run App without access to the phones file 
storage');
                       break;
               }
           }, 
           function(error){
               console.error("The following error occurred: "+error);
           }, 
           cordova.plugins.diagnostic.permission.WRITE_EXTERNAL_STORAGE
       );
   }
   function saveFile(dirEntry, fileData, fileName) {
       dirEntry.getFile(fileName, { create: true, exclusive: false }, function 
(fileEntry) {
         
           writeFile(fileEntry, fileData);
       }, onErrorCreateFile);
       function onErrorCreateFile() {
         alert("could not create file");
       console.log("Create file fail...");
       }
   
   }
   
   function writeFile(fileEntry, dataObj, isAppend) {
       // Create a FileWriter object for our FileEntry (log.txt).
       fileEntry.createWriter(function (fileWriter) {
   
           fileWriter.onwriteend = function() {
               alert("write successful");
               console.log("Successful file write...");
               alert(typeof(dataObj));
                   readBinaryFile(fileEntry);
               
           };
           fileWriter.onerror = function(e) {
               console.log("Failed file write: " + e.toString());
           };
   
           fileWriter.write(dataObj);
       });
   }
   
   function readBinaryFile(fileEntry) {
       fileEntry.file(function (file) {
           var reader = new FileReader();
   
           reader.onloadend = function() {
   
               console.log("Successful file write: " + this.result);
               displayFileData(fileEntry.fullPath + ": " + this.result);
               var blob = new Blob([new Uint8Array(this.result)], { type: 
"image/jpg" });
               displayImage(blob);
           };
   
           reader.readAsArrayBuffer(file);
   
       }, onErrorReadFile);
       function displayFileData(data){
   alert(data);
   }
       function onErrorReadFile() {
       console.log("Create file fail...");}
   
   function onErrorLoadFs() {
       console.log("File system fail...");
   }
   }
   
   function displayImage(blob) {
       // Displays image if result is a valid DOM string for an image.
       var elem = document.getElementById('imageFile');
       // Note: Use window.URL.revokeObjectURL when finished with image.
       elem.src = window.URL.createObjectURL(blob);
   }
     return PageModule;
   });
   
   ### Environment, Platform, Device
   <!-- In what environment, on what platform or on which device are you 
experiencing the issue? -->
   
   
   
   ### Version information
   <!-- 
   What are relevant versions you are using?
   For example:
   Cordova: Cordova CLI, Cordova Platforms, Cordova Plugins 
   Other Frameworks: Ionic Framework and CLI version
   Operating System, Android Studio, Xcode etc.
   -->
   
   
   
   ## Checklist
   <!-- Please check the boxes by putting an `x` in the `[ ]` like so: `[x]` -->
   
   - [ ] I searched for already existing GitHub issues about this
   - [ ] I updated all Cordova tooling to their most recent version
   - [ ] I included all the necessary information above
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to