Ionic3:Base64 To Gallery Plugin崩溃了应用程序

时间:2018-05-10 08:49:20

标签: javascript image cordova ionic3

我正在尝试将base64字符串保存到库中。当我调用这个插件时,我的代码崩溃了。以下是我用于check的链接。

我使用的代码

awk -F, -v fields="1,2,3,4,5,9" '              ##Setting field seprator as comma here with -F. Setting variable named fields with values of fields which we need.
BEGIN{                                         ##Starting BEGIN section here for awk which will be executed before reading the Input_file.
  num=split(fields, array,",")}                ##using split to split the variable fields into array named array and creating variable num which will have number of element of array.
{
  for(i=1;i<=num;i++){                         ##Starting a for loop here which starts from variable named i value from 1 to till value of variable num.
    printf("%s%s",$array[i],i==num?ORS:OFS)}}  ##Printing value of array[i] and then $array[i] will print the field value in current line too. Then checking condition variable i value equal to variable num then print new line else print space with OFS.
' OFS=,  Input_file                            ##Mentioning the Input_file name here.
  

我无法调试

我无法理解为什么我的应用会在点击此代码后自动关闭

更新

安装此特定版本的插件后

let options:Base64ToGalleryOptions = { prefix: '_img',mediaScanner: true }

   //after the below line my gets close automatically any idea
      this.base64ToGallery.base64ToGallery(base64Image[1],options)
      .then(
        res => {
          debugger
          console.log('Saved image to gallery ', res)
        },
        err => {
          debugger
          console.log('Error saving image to gallery ', err)
        });

将我的代码移至平台

 ionic cordova plugin add cordova-base64-to-gallery@2.0.2

但是这个相同的代码不适用于ios,根据我安装了相同版本的ios也支持ios(2.0.2),但它看起来有些东西丢失,如果有的请告诉我

1 个答案:

答案 0 :(得分:-1)

由于你无法在这里调试我遇到的三个问题,直到我开始工作,很可能是第二个问题,如果在Android上或第三个问题,如果在iOS上。

1)Error saving image to gallery cordova_not_available

修复此问题是创建一个使用命令ionic start blank --cordova

烘焙cordova的项目

2)Error saving image to gallary Error while saving image我在Android设备上收到此错误消息。我在这里查看了他们的代码实现https://github.com/Nexxa/cordova-base64-to-gallery/blob/2f531aaa0bf17b900cf6bd9704082e72f183d325/src/android/Base64ToGallery.java

看到他们没有做任何关于WRITE_EXTERNAL_STORAGE权限的事情。

我的解决方案是添加AndroidPermissions并在运行时检查WRITE_EXTERNAL_STORAGE权限。

hasWriteAccess: boolean = false;
constructor(private base64ToGallery: Base64ToGallery,
   private androidPermissions: AndroidPermissions) {
}

ionViewWillEnter() {
   this.checkPermissions();
}

checkPermissions() {
   this.androidPermissions
   .checkPermission(this.androidPermissions
   .PERMISSION.WRITE_EXTERNAL_STORAGE)
   .then((result) => {
    console.log('Has permission?',result.hasPermission);
    this.hasWriteAccess = result.hasPermission;
  },(err) => {
      this.androidPermissions
        .requestPermission(this.androidPermissions
        .PERMISSION.WRITE_EXTERNAL_STORAGE);
   });
   if (!this.hasWriteAccess) {
     this.androidPermissions
       .requestPermissions([this.androidPermissions
       .PERMISSION.WRITE_EXTERNAL_STORAGE]);
   }
}

saveImage() {
   if (!this.hasWriteAccess) {
     this.checkPermissions();
   }
   let options: Base64ToGalleryOptions = {
     prefix: '_img', 
     mediaScanner: true
   };
   this.base64ToGallery
     .base64ToGallery(this.base64Data, options).then(
     res => console.log('Saved image to gallery:', res),
     err => console.log('Error saving image to gallery:', err)
   );
}

3)This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSPhotoLibraryAddUsageDescription key with a string value explaining to the user how the app uses this data.

解决方案是将NSPhotoLibraryAddUsageDescription添加到嵌套在<platform name="ios"> and </platform>

之间的project_name / config.xml
<config-file parent="NSPhotoLibraryAddUsageDescription" target="*-Info.plist">
    <string>Saves images from base64 to your Photo Library</string>
</config-file>