我从galary中选择图像并保存到app目录中,但是当我选择图像时,我在url中的文件名是随机数,如:
"/media/external/images/media/26183"
我没有得到确切的文件名,因此当文件要复制文件时找不到错误就是抛出我的代码来获取和保存:
$scope.ChangeProfile = function() {
var options = {
quality: 30,
destinationType: Camera.DestinationType.NATIVE_URI,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
encodingType: Camera.EncodingType.JPEG,
mediaType:Camera.MediaType.PICTURE
};
navigator.camera.getPicture( OnCameraSuccess, OnCameraError, options);
};
function OnCameraSuccess(imageURI) {
console.log(imageURI);
window.resolveLocalFileSystemURL(imageURI,OnSucessFilePath, onErrorFilePath);
}
function onErrorFilePath(error){
console.log(error);
}
function OnSucessFilePath(fileEntry){
console.log(fileEntry);
var FilePath=fileEntry.nativeURL;
var name = FilePath.substr(FilePath.lastIndexOf('/') + 1);
var namePath = FilePath.substr(0, FilePath.lastIndexOf('/') + 1);
var newName = "Profile_Pic";
$cordovaFile.removeFile(cordova.file.dataDirectory, newName)
.then(function (success) {
console.log(success);
}, function (error) {
console.log(error);
});
// var newName = makeid() + name;
$cordovaFile.copyFile(namePath, name, cordova.file.dataDirectory, newName)
.then(function(info) {
console.log(info);
ProfileImage.Image = cordova.file.dataDirectory+newName;
$scope.PicUpload = true;
},
function(err){
console.log(err);
});
}
function OnCameraError(err){
console.log(err);
$ionicLoading.show({template: 'error in Loading', duration:500});
}
请告诉我我在哪里做错了,我花了很多时间在这上面,谢谢你。
答案 0 :(得分:0)
如果您在Android平台上运行应用程序,那么我认为您需要解析本机uri。您可以使用this plugin解决此问题。
示例:的
window.FilePath.resolveNativePath(YourNativeURI,
function (result) {
console.log('Resolving file path: ' + result);
}, function (error) {
console.log(error);
});