How to move file to app directory using Cordova

那年仲夏 提交于 2019-12-07 23:25:15

问题


How can I move the image captured from my phone camera to the Cordova app directory?

I want to move the file to, ideally with mkdir like function for new directories

app/www/uploads/new

The image location of the file is saved in a var:

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

Any help is welcomed.

I am using Cordova version 4.1.2 and installed the Cordova File Plugin.


回答1:


I do think that the app/www directory is read-only since it contains your actual code and is packaged as .apk. Why would you need it there though? It is still since it is on disk. If you want to copy it for your own applications data partition, you should use the File plugin that you already mentioned to move it under cordova.file.externalDataDirectory or some other place you wish to move it.

Update

Since you want to show the file as image on HTML, the easiest way is to read it as Data URL (base64 format) and give that as src for image instead of path for 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 for that code is borrowed from the Pratik Sharma's answer and it may not completely work for your case but you should be able to pick up the idea from there. It relies on that you have this kind of image tag available on your HTML.

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

If you want to read more about this, consult this Wikipedia article.



来源:https://stackoverflow.com/questions/27765007/how-to-move-file-to-app-directory-using-cordova

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!