cordova-plugin-crop icon indicating copy to clipboard operation
cordova-plugin-crop copied to clipboard

Android 7+ problem in save cropped image

Open brunosoaresds opened this issue 6 years ago • 10 comments

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:

  • https://forum.ionicframework.com/t/cant-open-pdf-with-fileopener2-with-ionic-on-android-7/88326/8
  • https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en

Best

brunosoaresds avatar Jan 15 '19 16:01 brunosoaresds

Same problem here!!

jorgegarciafon avatar Jan 25 '19 10:01 jorgegarciafon

Same issue.

T0shik avatar Feb 03 '19 20:02 T0shik

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

lfreneda avatar Feb 05 '19 15:02 lfreneda

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.

brunosoaresds avatar Feb 05 '19 15:02 brunosoaresds

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.

draco1989 avatar Feb 05 '19 15:02 draco1989

@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.

brunosoaresds avatar Feb 05 '19 15:02 brunosoaresds

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 avatar Feb 05 '19 16:02 lfreneda

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

image

lfreneda avatar Feb 05 '19 16:02 lfreneda

@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.

draco1989 avatar Feb 05 '19 16:02 draco1989

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;
	}
}

allanpoppe avatar May 09 '19 18:05 allanpoppe