anshuman61 commented on issue #426:
URL: 
https://github.com/apache/cordova-plugin-file/issues/426#issuecomment-906139050


   Actually i have the same problem while i upgraded to API 30. I got error 
code 9 also. As google upgraded its android protocol set for storage we have to 
use "scoped storage".  
   
   1. In my application i have to capture image, then i have to compress using 
Javascript image compressors (usually gives better quality than the 
"`cordova-plugin-camera`" compressors and have more flexibility)  and have to 
write compressed  files using "`cordova-plugin-file`" to storage.
   
   2. Previously i am using "`LocalFileSystem.PERSISTENT`" and with API 30 i 
couldn't access that region of my file system due to restrictions imposed.
   
   Example:-
    `window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fs) { 
   console.log('file system open: ' + 
fs.name);fs.root.getFile("newPersistentFile.txt", { create: true, exclusive: 
false }, function (fileEntry) {console.log("fileEntry is file?" + 
fileEntry.isFile.toString());
           // fileEntry.name == 'someFile.txt'
           // fileEntry.fullPath == '/someFile.txt'
           writeFile(fileEntry, null);}, onErrorCreateFile); }, onErrorLoadFs);`
   
   
   
   3. Then i changed  "`LocalFileSystem.PERSISTENT`"  with  
"`cordova.file.dataDirectory`" and it worked. Its also logically correct as per 
the restrictions imposed now a days.  
   
   Example :- 
   
   
    `window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function 
(dirEntry) {
       console.log('file system open: ' + dirEntry.name);
       var isAppend = true;
       createFile(dirEntry, "fileToAppend.txt", isAppend);
   }, onErrorLoadFs);`
   
   
   
   This example can also be used to create new files, if you provide unique 
file name each time. Like below.
   
   Example :- 
   
   `
   window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function(fs) {
   createFile(fs, outputfilename);
                           function createFile(dirEntry, fileName, isAppend) {
                               // Creates a new file or returns the file if it 
already exists (use unique file name each time) .
                               dirEntry.getFile(fileName, { create: true, 
exclusive: false }, function(fileEntry) {writeFile(fileEntry, result);
                               }, function(err) {
                                   console.log(err);
                               });}function writeFile(fileEntry, dataObj) {
                               // Create a FileWriter object for our FileEntry 
(log.txt).
                               fileEntry.createWriter(function(fileWriter) 
{fileWriter.onwriteend = function() {console.log("File written 
successfully...");console.log(fileEntry); };fileWriter.onerror = function(e) {
                                       console.log("Failed to write file: " + 
e.toString());
                                   };// If data object is not passed in,
                                   // create a new Blob instead.
                                   if (!dataObj) {
                                       dataObj = new Blob(['some file data'], { 
type: 'text/plain' });
                                   }fileWriter.write(dataObj);
                               });
                           } }, function(error) { console.log(error); });`
    
   
   Correct me if something i missed. Thank you all. 


-- 
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.

To unsubscribe, e-mail: issues-unsubscr...@cordova.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@cordova.apache.org
For additional commands, e-mail: issues-h...@cordova.apache.org

Reply via email to