Cordova Camera and File Plugin Issue on New Android system

Take a picture, Move the file to MY App’s Photo folder…Always worked, no longer on new Android 13.

The photo plugin returns "fileURI " = file:///data/user/0/com.nsbasic.Vantastic/cache/1697768037972.jpg
(inside the cache folder of the apps sandbox). Simply passing the above file URI to the getfile() method returns a not found error. How can i convert the above “fileURI” to something that the getfile() can find? I have tried:

  1. window.resolveLocalFileSystemURL(fileURI)
  2. window.resolveLocalFileSystemURL(cordova.file.cacheDirectory) + “1697768037972.jpg”
  3. toURL(fileURI)
    Any ideas??

This code used to work, now fails wiyth code :1 — failst() below

//—Begin Photo Stuff
Mimage.onclick = function() {
options = {
quality: 75,
correctOrientation: true,
allowEdit: false,
destinationType: 1
};
navigator.camera.getPicture(onPictureReturned, onPictureError, options);
};
function onPictureReturned(fileURI) {
varLastSlash = InStrR(fileURI, “/”, 0);
varfileURI = fileURI;
console.log(varfileURI);
PhotoName = Mid(fileURI, varLastSlash); //The Photo name
varPicture = Trim(gSelectedBroker) + “-” + Trim(gSelectedCategoryNumber) + “-” + Trim(gSelectedChainNumber) + “-” + Trim(gSelectedStoreNumber) + “-” + CStr(Year(varDate)) + PhotoMonth + PhotoDay + “-” + Trim(varQuestionNO);
varPicture = varPicture + “.jpg”;
MovePicS();
}
function onPictureError(message) {
NSB.MsgBox("Failed because " + message);
}
function MovePicS() {
gfileSystem.root.getFile(varfileURI, {
create: false
}, gotFileEntryS, fail2st);
}
function gotFileEntryS(fe) {
FileEntry = fe;
FileEntry.moveTo(gdataDir, varPicture, SubSuccess2, fail3st);
}
function SubSuccess2(entry) {
//MsgBox “Successfully Moved file from Cache:” & vbCRLF & PhotoName & vbCRLF & "To Photos: " & varPicture
NSB.MsgBox(“Press [SAVE ANSWER] to Complete Process…”, 0, “Photo Successfull”);
}
function fail2st(error) {
console.log(error);
NSB.MsgBox(“Error getFile(PhotoName: " + error.code + " -” + error.error + “-” + PhotoName);
}
function fail3st(error) {
NSB.MsgBox("Error .moveTo(gdataDir: " + error.code + " – " + error.error + “-” + varPicture);
}
//—End Photo Stuff

Can you share how you’ve been using resolveLocalFileSystemURL?

My Bad: Here is the VB Code, ignore above!

Global Page:

Dim gfileSystem  -- Successfully retrieved from : 
Function onDeviceReady()
  window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFileSys, fail0)
End Function
Function gotFileSys(fs)
  gfileSystem = fs  'The fileSystem object
End Function
----------------------------------------------

Dim FileEntry   'The file object
Dim PhotoName   'The generated name by the android camera function
Dim varPicture   'New Name of photo
Dim varfileURI
Function onISPictureReturned(fileURI)
  varLastSlash = InStrR(fileURI, "/", 0)
  varfileURI = fileURI
console.log(fileURI)
  'varROOT = InStr(1,fileURI, "Android",0)
  'PhotoName = Mid(fileURI, varROOT)  'The PhoneGap root file name
  PhotoName =  Mid(fileURI, varLastSlash + 1)
  console.log("PhotoName Only:" + PhotoName)
   'Try this?
  varDate = FormatDateTime(Now, vbYYYYhyphenMMhyphenDD)
varPicture = Trim(gSelectedBroker) & "-" &  Trim(gSelectedChainNumber) & "-" & Trim(gSelectedStoreNumber) & "-" & gUPC & "-" & varDate
varPicture = varPicture & ".jpg"
Call MovePicIS()
End Function

Function MovePicIS()
'  varEntry = window.FilePath.resolveNativePath(varfileURI)
'  varEntry =  window.resolveLocalFileSystemURL(varfileURI)
'  varDirectory = varEntry
  fsTemp = window.requestFileSystem(window.TEMPORARY,5 * 1024 * 1024)
    console.log("Full Path Parameter to get file: " + varDirectory + PhotoName)
  gfileSystem.root.getFile(varfileURI, { create: False }, gotFileEntryIS, fail2IS)
End Function
Function gotFileEntryIS(fe)
  FileEntry = fe
 ' FileEntry.moveTo(gdataDir, varPicture, SubSuccess2IS, fail3IS)
varDirectory = window.resolveLocalFileSystemURL(cordova.file.dataDirectory)
varDirectory = varDirectory + "Photos"
FileEntry.moveTo(varDirectory, varPicture, SubSuccess2IS, fail3IS)
'Three lines above are a try
End Function

Function SubSuccess2IS(entry)
   'NSB.MsgBox("Use [Send Data] to Process All Photos to HQ",0,"Photo Succesfully Stored") 
End Function
Function onISPictureError(message)
  MsgBox "Failed because " & message
End Function
Function fail2IS(error)
  MsgBox "Error getting: " + error.code + " --" + PhotoName
End Function
Function fail3IS(error)
 MsgBox "Error writing: " + error.code + " -- " + varPicture 
End Function

I found the name of the photo and set:

varEntry, or varfileURI  = window.resolveLocalFileSystemURL(cordova.file.cacheDirectory) + PhotoName   ( i.e.:  “1697768037972.jpg” )

or

varfileURI = window.resolve.LocalFileSystemURL(varfileURI)  -- The URI returned from the PhotoPlugin

And used it in the function:

gfileSystem.root.getFile(varfileURI, { create: False }, gotFileEntryIS, fail2IS)

I think I see the issue here. resolveLocalFileSystemURL isn’t a synchronous function. It needs a callback:

window.resolveLocalFileSystemURL(varfileURI, function(entry) {
  console.log("Here's the path: " + entry)
}

Give that a try and see if you have better luck.

That appears to error out syntax.
:Uncaught SyntaxError: missing ) after argument list
I tried enclosing in javascipt/End Java script. Below is what your Translator returns in java code:

window.resolveLocalFileSystemURL(varfileURI, Function(entry) {
        console.log("Here's the path: " + entry)
    }
    console.log("Full Path Parameter to get file: " + varDirectory + PhotoName); gfileSystem.root.getFile(entry, {
        create: false
    }, gotFileEntryIS, fail2IS);
}

What is the equivalent in VBScript??

Thanks!
Jimmy

VBScript doesn’t have anonymous functions so you’ll need to make a function somewhere else, like so:

Function getFilePath(entry)
  console.log("Here's the path: " & entry)
End Function

window.resolveLocalFileSystemURL(varfileURI, getFilePath)

James. Thanks for your reply. It’s funny because I used that method to create the directories PDFs and Photos under the files directory in the sandbox. Nevertheless, I used your method:

  navigator.camera.getPicture(onISPictureReturned, onISPictureError)

End Function

Function onISPictureReturned(fileURI)
  varLastSlash = InStrR(fileURI, "/", 0)
  varfileURI = fileURI
'--------------*(see 1. Below)* 
console.log(fileURI)
  PhotoName =  Mid(fileURI, varLastSlash + 1)
'-------------*(see 2. Below)* 
 console.log("PhotoName Only:" + PhotoName)
  varDate = FormatDateTime(Now, vbYYYYhyphenMMhyphenDD)
varPicture = Trim(gSelectedBroker) & "-" &  Trim(gSelectedChainNumber) & "-" & Trim(gSelectedStoreNumber) & "-" & gUPC & "-" & varDate
varPicture = varPicture & ".jpg"
Call GetEntry()
End Function

Function GetEntry()
window.resolveLocalFileSystemURL(varfileURI, MovePicIS(entry) )
End Function
MovePicIS(entry)
'-------------*(see 3. Below)*   
console.log("Path Parameter to get file: " + entry)
    gfileSystem.root.getFile(entry, { create: False }, gotFileEntryIS, fail2IS)
End Function
Function gotFileEntryIS(fe)
FileEntry = fe
varDirectory = window.resolveLocalFileSystemURL(cordova.file.dataDirectory)
varDirectory = varDirectory + "Photos"
FileEntry.moveTo(varDirectory, varPicture, SubSuccess2IS, fail3IS)
'Three lines above are a try
End Function

Function SubSuccess2IS(entry)
   'NSB.MsgBox("Use [Send Data] to Process All Photos to HQ",0,"Photo Succesfully Stored") 
End Function
Function onISPictureError(message)
  MsgBox "Failed because " & message
End Function
Function fail2IS(error)
  MsgBox "Error getting: " + error.code + " --" + PhotoName
End Function
Function fail3IS(error)
 MsgBox "Error writing: " + error.code + " -- " + varPicture 
End Function

and I am getting an error Code # 5 ENCODING_ERR: here is the console output:

1.  file:///data/user/0/com.nsbasic.Vantastic/cache/1698757766619.jpg
2. PhotoName Only:1698757766619.jpg
3. Full Path Parameter to get file: undefined1698757766619.jpg ( undefined is my attempt to get error description)

Tip : If you’re pasting code, html or config files, surround the code with triple back ticks (```), before the first line and after the last one. It will be formatted properly. (We fixed it for you this time)

i should have triple-ticked the code block…next time. IN:
Path Parameter to get file: undefined1698757766619.jpg
the undefined is not the error description…it appears to be the directory where the cache directory where the photo was originally stored : file:///data/user/0/com.nsbasic.Vantastic/cache/ … weird how it is undefined BUT the file name itself is still in that string.

I think the following line is the issue:

window.resolveLocalFileSystemURL(varfileURI, MovePicIS(entry) )

It should be:

window.resolveLocalFileSystemURL(varfileURI, MovePicIS )

Give that a try and see how it works.