How to transfer a file to the cordova application directory

How to transfer the image from the phone camera to the cordova application directory?

I want to move the file ideally using the mkdir function for new directories

app/www/uploads/new 

File image location saved in var

 imagePath = file://emu/0/android/cache/something.jpg 

any help is appreciated.

I am using Cordova version 4.1.2 The Cordova file plugin is also installed

0
source share
1 answer

I really think the app/www directory is read-only, as it contains your actual code and is packaged as .apk. Why do you need this? This is still since it is on disk. If you want to copy it for your own application data section, you must use the File plugin that you already mentioned to move it in the cordova.file.externalDataDirectory file or in another place you want to move.

Update

Since you want to show the file as an image in HTML, the easiest way is to read it as a data URL (base64 format) and provide it as src for the image instead of the path for a file like this

 window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, onFail);} function onFail(message) { alert('Failed because: ' + message); } function gotFS(fileSystem) { fileSystem.root.getFile("<your_image_file>.jpg", null, gotFileEntry, fail); } function gotFileEntry(fileEntry) { fileEntry.file(gotFile, fail); } function gotFile(file){ readDataUrl(file); } function readDataUrl(file) { var reader = new FileReader(); reader.onloadend = function(evt) { console.log("Read as data URL"); console.log(evt.target.result); var imageData = evt.target.result; document.getElementById("img#image1").src = imageData; }; reader.readAsDataURL(file); // Read as Data URL (base64) } 

The structure of this code is borrowed from Pratik Sharma 's answer, and it may not fully work for your business, but you should be able to take this idea from there, It relies on the fact that you have such an image tag available on your HTML.

 <img id="image1" src="" /> 

If you want to know more about this, refer to this Wikipedia article.

+1
source

Source: https://habr.com/ru/post/1241696/


All Articles