How to move file to app directory using Cordova

可紊 提交于 2019-12-06 12:27:32
Roope Hakulinen

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.

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