如何使用Cordova将文件移动到应用程序目录 [英] How to move file to app directory using Cordova

查看:174
本文介绍了如何使用Cordova将文件移动到应用程序目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将从手机摄像头捕获的图像移动到Cordova应用程序目录中?

我想将文件移至,最好使用mkdir之类的新目录功能

app/www/uploads/new

文件的图像位置保存在var中:

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

欢迎任何帮助.

我正在使用Cordova 4.1.2版并安装了Cordova文件插件.

我确实认为app/www目录是只读的,因为它包含您的实际代码并且打包为 .apk .您为什么在那里需要它?仍然是因为它在磁盘上.如果要将其复制到自己的应用程序数据分区,则应使用已经提到的 File插件将其移动到 cordova.file.externalDataDirectory 或其他位置您想移动它.

更新

由于要在HTML上将文件显示为图像,所以最简单的方法是将其读取为数据URL (base64格式),然后将其作为 src 而不是像这样的文件的路径

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)
}

该代码的结构是从借用的Pratik Sharma的回答,可能无法完全解决您的问题,但您应该能够从中获得灵感.它依赖于您的HTML上具有这种图像标签.

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

如果您想了解更多有关此的内容,请查阅这篇维基百科文章.

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.

解决方案

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.

这篇关于如何使用Cordova将文件移动到应用程序目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆