0

我正在使用Cordova 插件相机来捕获用户的个人资料图片。我想将其存储在应用程序中。

这是捕获它的代码:

 Camera.getPicture({
      quality : 75,
      destinationType : Camera.DestinationType.DATA_URL,
      sourceType : Camera.PictureSourceType.CAMERA,
      allowEdit : true,
      encodingType: Camera.EncodingType.JPEG,
      targetWidth: 300,
      targetHeight: 300,
      saveToPhotoAlbum: false
    }).then(imageData => {
      this.base64Image = "data:image/jpeg;base64," + imageData;

    }, error => {
      console.log("ERROR -> " + JSON.stringify(error));
    });

捕获图像后,我想将其存储在本地,以便可以将其显示在个人资料页面上。任何线索我怎么能做到这一点?

4

2 回答 2

1

您可以使用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
      // ...
 });
于 2016-07-22T03:39:17.250 回答
0

您可以使用这些选项:

Camera.getPicture({
    sourceType: 1, // camera
    destinationType: 1, // file uri
    correctOrientation: true,
    saveToPhotoAlbum: true,
    encodingType: 0, // jpeg
}).then((imageUri) => {
});

拍摄的图像将被自动保存。然后用于imageUri显示图像。如果要读取文件内容,请参阅http://ionicframework.com/docs/v2/native/file/

于 2016-07-22T03:49:27.323 回答