jeduan / cordova-plugin-crop

Crop an image in Cordova
MIT License
114 stars 170 forks source link

Android 7+ problem in save cropped image #72

Open brunosoaresds opened 5 years ago

brunosoaresds commented 5 years ago

Hi all,

I'm facing a problem of FILE NOT FOUND when i use this library in android 7+. After search, i see that api 24 have changed the access to file:// and maybe because of that this problem is occurring. When i set the targetSdk to version 23 everything works fine, but google play only accepts now apps with targetSdk 26+. Unfortunately i have not found a solution for this problem, can anyone help?

Related problem:

Best

jorgegarciafon commented 5 years ago

Same problem here!!

T0shik commented 5 years ago

Same issue.

lfreneda commented 5 years ago

@brunosoaresds same issue here, did you fixed it somehow?

brunosoaresds commented 5 years ago

No, unfortunatelly i didn't fix it yet. Actually in my app i have disabled crop in android 7+. I hope someone fix it asap.

draco1989 commented 5 years ago

Hi, I have been trying to solve it for a couple of days and best way I found to avoid the error is this:

private cropDevice(options: ImageTransformationOptions) {
    return (cameraResult: Observable<string>) => cameraResult.pipe(
      switchMap(url => this.crop(url, {
        quality: 100,
        targetWidth: options.crop.width,
        targetHeight: options.crop.height,
        destinationType: DestinationType.DATA_URL,
        encodingType: EncodingType.JPEG
      })),
      // USE Cordova File Plugin to read file
      switchMap(url => new Promise((resolve, reject) =>
        (window as any).resolveLocalFileSystemURL(
          url,
          (file: any) => file.file((data: File) => resolve(data)),
          (err: Error) => reject(err)))),
      // USE browser FileReader to convert it to base64 stirng
      switchMap((file: File) => new Promise((resolve, reject) => {
        const reader = new FileReader();
        reader.onload = (event: any) => resolve(event.target.result);
        reader.onerror = err => reject(err);
        reader.readAsDataURL(file);
      }))
    );
  }

It seems that saved crop image is being saved in local fs and then in app browser is not capable of querying it. So the solution is to use Cordova File Plugin and you are done.

It's working from Android 5.0.1 to Android 9. I didn't try older versions.

brunosoaresds commented 5 years ago

@draco1989 did you have tested using the Cordova File Plugin readAsDataURL method instead of using the FileReader? maybe the readAsDataURL can accept the file:// archive too.

lfreneda commented 5 years ago

Weirdo, but installing https://github.com/apache/cordova-plugin-file started to work as expected D:

Permissions, perhaps?

I'm going to upload a working sample for you guys.

Thanks everybody! :dancing_women:

lfreneda commented 5 years ago

:framed_picture: Check this out: https://github.com/lfreneda/ionic-crop-image-demo

image

draco1989 commented 5 years ago

@brunosoaresds That piece of code is just a partial of my code, and I have more steps between, but yes, you can use that method to get base64.

allanpoppe commented 5 years ago

Maybe you need to use cordova-plugin-android-permissions to check and request for WRITE_EXTERNAL_STORAGE permission before selecting image.

if (this.platform.is('android')) {
    try {
        const permission = await this.androidPermissions.checkPermission(this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE);
        if (!permission.hasPermission) throw new Error('no-permission');
    } catch (e) {
        const requestPermission = await this.androidPermissions.requestPermission(this.androidPermissions.PERMISSION.WRITE_EXTERNAL_STORAGE);
        if (!requestPermission.hasPermission) return;
    }
}