如何将文件移动到应用程序目录cordova [英] How to move file to app directory cordova
问题描述
如何将我手机相机拍摄的图像移动到cordova应用程序目录?
我想将文件移动到mkdir,目录
app / www / uploads / new
文件的映像位置保存在var
中。 imagePath = //emu/0/android/cache/something.jpg
欢迎任何帮助。
我使用Cordova版本4.1.2
还安装了Cordova文件插件
我认为 app / www
目录是只读的,因为它包含你的实际代码,并打包为 .apk 。为什么你需要它在那里?它仍然是因为它在磁盘上。如果您要将其复制到自己的应用程序数据分区,应使用您已提到的文件插件将其移动到 cordova.file.externalDataDirectory 或其他位置
由于您要显示文件作为HTML上的图像,最简单的方法是将其读为数据URL (base64格式),并将其作为 src p>
window.requestFileSystem(LocalFileSystem.PERSISTENT,0,gotFS,onFail);}
function onFail ){
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 =image1src =/>
如果您想了解更多信息,请参阅这维基百科文章。
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 welcome.
I am using Cordova version 4.1.2 Also 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屋!