我正在开发Onsen / Cordova移动应用程序,使用Monaca进行开发,使用Android手机进行测试,将照片附加到应用程序的输出。我有这个代码拍照并将其复制到cordova.file.dataDirectory:
navigator.camera.getPicture(
function(imgUri) { // Success callback
// Get FileEntry object for the new photo
window.resolveLocalFileSystemURL(
imgUri,
function(fileEntry){
alert('Success - Before: ' + JSON.stringify(fileEntry));
newFileName = fileEntry.name;
window.resolveLocalFileSystemURL(cordova.file.dataDirectory,
function(dirEntry) {
// move the file to a new directory and rename it
fileEntry.moveTo(dirEntry, newFileName, function success() {
alert('Copy operation successful - After moving: ' + JSON.stringify(fileEntry)
+ '\nURL: ' + fileEntry.toURL());
$scope.photos.unshift(fileEntry.toURL());
$scope.$apply();
},
function failure(){
alert('Failed to move file.');
});
},
function failure(){
alert('Failed to get local FS for data directory.');
});
},
function failure(){
alert('Failed to get local FS for new image.');
});
},
function() { // Error callback
// We end up here if the photo selection/shoot is cancelled. So we do nothing.
return;
},
opts // Options
);
不幸的是,虽然触发了所有成功的回调,但警报会在移动照片之前和之后显示相同的路径 - file:// storage / emulated / 0 / Android / data / mobi.monaca.debugger / cache / - 而cordova.file.dataDirectory的路径是 file:///data/user/0/mobi.monaca.debugger.files/ 。
此代码中有什么不正确?
答案 0 :(得分:1)
您是否也尝试过'copyTo'而不是'moveTo'?我已经阅读了我的旧代码,我看到我使用copyTo。我记得moveTo被操作系统阻止了(在某处读取了googlin),但我真的不知道哪个Android版本。 如果'copyTo'功能适合你,这可能就是原因。
编辑:这是我的工作代码。检查并查看差异(文件系统可能?)
function manipulate_img_url(fileUri) {
console.log("fileUri: "+fileUri);
window.requestFileSystem(PERSISTENT, 0, function(fileSystem) {
window.resolveLocalFileSystemURL(
fileUri,
function(fileEntry){
console.log("Start elaboration: "+JSON.stringify(fileEntry)); // This give me the native url: 'nativeURL':"file:///storage/emulated/0/Android/data/com.intel.appx.RotarApp.xwalk15/cache/1465151572273.jpg"
newFileUri = cordova.file.dataDirectory + "images/";
console.log("newFileUri: "+newFileUri);
oldFileUri = fileUri;
console.log("oldFileUri: "+oldFileUri);
fileExt = oldFileUri.split('.').pop();
console.log("fileExt: "+fileExt);
newFileName = "my_avatar" + fileExt;
console.log("newFileName: "+newFileName);
window.resolveLocalFileSystemURL(cordova.file.dataDirectory,
function(dirEntry) {
// move the file to a new directory and rename it
fileEntry.copyTo(dirEntry, newFileName, successCallback, errorCallback);
},
errorCallback);
},
errorCallback);
}, function(e) {
console.error('Error requesting file system: '+ e.code);
});
}
function successCallback(entry) {
console.log("entry: "+JSON.stringify(entry));
console.log("New Path: " + entry.fullPath);
console.log("native url: "+entry.nativeURL); // <<--- this is the new url: file:///data/data/com.intel.appx.RotarApp.xwalk15/files/my_avatar.jpg
}