Cordova使用文件url移动文件 [英] Cordova Move File using the file url
问题描述
如何使用我从相机获得的网址移动文件?
How can I move a file using the URL I get from the Camera?
既不是successCallback也不是errorCallback由函数moveTo调用。任何人都可以告诉我我在做什么错误,可能的解决方案是什么样子?
neither successCallback nor errorCallback is called by the function moveTo. Can anyone tell me what I am doing wrong and what a possible solution looks like?
function successCallback(entry) {
console.log("New Path: " + entry.fullPath);
alert("Success. New Path: " + entry.fullPath);
}
function errorCallback(error) {
console.log("Error:" + error.code)
alert(error.code);
}
// fileUri = file:///emu/0/android/cache/something.jpg
function moveFile(fileUri) {
newFileUri = cordova.file.dataDirectory + "images/";
oldFileUri = fileUri;
fileExt = "." + oldFileUri.split('.').pop();
newFileName = guid("car") + fileExt;
// move the file to a new directory and rename it
fileUri.moveTo(cordova.file.dataDirectory, newFileName, successCallback, errorCallback);
}
我使用Cordova版本4.1.2也安装了Cordova文件插件
I am using Cordova version 4.1.2 Also installed the Cordova File Plugin
推荐答案
您尝试在String上调用moveTo函数。
You're trying to call the function moveTo on a String.
moveTO 不是String的函数,而是fileEntry的函数。所以你需要做的第一件事是从你的URI中获取一个fileEntry。
moveTO is not a function of String but of fileEntry. So first thing you need to do is get a fileEntry from your URI.
为此,你需要调用 window.resolveLocalFileSystemURL :
function moveFile(fileUri) {
window.resolveLocalFileSystemURL(
fileUri,
function(fileEntry){
newFileUri = cordova.file.dataDirectory + "images/";
oldFileUri = fileUri;
fileExt = "." + oldFileUri.split('.').pop();
newFileName = guid("car") + fileExt;
window.resolveLocalFileSystemURL(newFileUri,
function(dirEntry) {
// move the file to a new directory and rename it
fileEntry.moveTo(dirEntry, newFileName, successCallback, errorCallback);
},
errorCallback);
},
errorCallback);
}
这篇关于Cordova使用文件url移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!