您可以使用cordova-plugin-file。我也会更改选项
destinationType : Camera.DestinationType.DATA_URL,
为了
destinationType: Camera.DestinationType.FILE_URI
(机器人)或
destinationType: Camera.DestinationType.NATIVE_URI
(IOS)
因为DATA_URL
可能会占用大量内存并导致应用程序崩溃或内存不足错误。如果可能,您应该使用FILE_URI
或。NATIVE_URI
所以这个想法是获取设备上图像的路径。这样,我们可以将该照片移动到为您的应用创建的文件夹(将永久存储),然后您可以使用该路径在个人资料页面中显示照片。当我使用这个 Cordova 插件文件时,它不是 Ionic Native 的一部分,所以代码有点难看......
Camera.getPicture(/*...theOptions*/).then(
(imagePath) => {
// Create a new file name by using the username or something like that
let aDate = new Date(),
aTime = aDate.getTime(),
userName = this.myCustomUserService.getUserName(),
newFileName = userName + aTime + ".jpg";
// Returns a reference to the actual file located at imagePath
window.resolveLocalFileSystemURL(imagePath,
(file) => {
// Returns a reference to the file system
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
(fileSys) => {
// Gets a reference to your app directory, and if it doesn't exist it creates it
fileSys.root.getDirectory('yourAppFolderName', {create: true, exclusive: false},
(directory) => {
// Moves the file to that directory, with its new name
file.moveTo(directory, newFileName,
// The file was succesfully moved and it returns a reference to it. We can use nativeURL to grab the
// path to the image on the device
(fileEntry) => {
this.myCustomUserService.SetProfilePhotoUrl(fileEntry.nativeURL);
},
// Now we start handing all the errors that could happen
(error) => {
// The file was unable to be moved
// Show error to the user
// ...
});
},
(error) => {
// Could not get a reference to your app folder
// Show error to the user
// ...
});
},
(error) => {
// Could not get a reference to the file system
// Show error to the user
// ...
});
});
},
(err) => {
// Could not take picture
// Show error to the user
// ...
});