How do I read/display an image from the app's temp folder in iOS.

When running my Ionic app natively (installed binary IPA) in an iOS, the Cordova-Barcode-Scanner generates an image file (when encoding text) and saves it locally.

My problem is that I need to display the image that was saved locally at:
"/private/var/mobile/Containers/Data/Applications//tmp/fileName.jpg" and I don't know how to do it with Cordova.

I looked at the Cordova-File-Plugin and tried using the 'cdvfile://localhost/temporary/path/to/fileName.jpg' path but I can't make it work.

Please help.

Thanks,
Rodrigo
1 person has
this question
+1
Reply
  • Hello,

    You can save it in the local application folder and find it in the folder like this table says: https://www.npmjs.com/package/cordova...

    You can find the similar topic here: https://stackoverflow.com/questions/3...
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited

  • Hi Serhii,

    Thanks for your response. I had already gone through the plugin's documentation but it's not clear to me how to call the file. If I use the File System, I would need to use cordova.file.tempDirectory (for iOS), but that would only get me to the directory. How do I call a specific file within the directory? Could you provide an example?

    Another option would be using the cdvfile path (as I described above). This option is also described in the plugin's documentation link you provided. However, I was not able to make it work either AND the documentation states that I would have to modify the index and config files (see image below).


    Following instructions, I modified index and config files but got the errors below:


    Thanks,
    Rodrigo
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated indifferent, undecided, unconcerned kidding, amused, unsure, silly happy, confident, thankful, excited

  • It works only on the real mobile device but doesn't work on the browser.
    You can see the example of it's usage in their github page:
    https://github.com/apache/cordova-plu...
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated indifferent, undecided, unconcerned kidding, amused, unsure, silly happy, confident, thankful, excited

  • Hi Serhii,

    I mentioned above that I am running the app natively on an iOS device.

    Following different examples I found, I came up with the code below but it is not working. PLEASE NOTE that I added the code as a Scope Function that runs onClick of a button and nothing is happening. At this point, I don't know if there is an issue with the code or if I put the code in the wrong place.

    1. Is the code below correct?
    2. Where should I place this code so it works?

    Code:
    var fileName = "image.jpg";

    function readFile(fileName) {
    var pathToFile = cordova.file.tempDirectory + fileName;
    window.resolveLocalFileSystemURL(pathToFile, function (fileEntry) {

    fileEntry.file(function (file) {

    var reader = new FileReader();
    reader.onload = function () {
    console.log("Successful file read: " + this.result);
    displayFileData(fileEntry.fullPath + ": " + this.result);
    };

    reader.readAsDataURL(file);
    }, alert("Error 1");
    }, alert("Error 2");
    }

    Thanks,
    Rodrigo
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited

  • You have to run that function "readFile" in your $scope function.
    Also, please check this code, it has an error: "Uncaught SyntaxError: missing ) after argument list"
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated indifferent, undecided, unconcerned kidding, amused, unsure, silly happy, confident, thankful, excited

  • I’m frustrated.
    Hi Serhii,
    I checked the code and fixed the missing ")"s but the code is still not working. I'm running it in a scope function but it's not doing anything.

    Do I need to pass the result to an element on the page? If so, how?

    I tried creating a scope variable and passing an output to it (see below) but it's not working. I need your help.

    Part of scope function here:
    reader.onloadend = function() {
    var dataURL = reader.result;
    var output = document.getElementById('output');
    output.src = dataURL;
    alert("dataURL: " + dataURL);
    alert("output: " + output);
    var output_scope = $scope.output;
    output_scope = output;
    $scope.output = output_scope;
    };

    Thanks,
    Rodrigo
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited

  • 1. You don't need to use page elements directly from your code, all data binding should be done only with scope variables.
    2. Please be sure you've gotten that value before using it (print it to the console or alert)
  • (some HTML allowed)
    How does this make you feel?
    Add Image
    I'm

    e.g. sad, anxious, confused, frustrated kidding, amused, unsure, silly indifferent, undecided, unconcerned happy, confident, thankful, excited