Skip to content
This repository has been archived by the owner on Sep 27, 2020. It is now read-only.

Android 7+ problem in save cropped image #72

Open
brunosoaresds opened this issue Jan 15, 2019 · 10 comments
Open

Android 7+ problem in save cropped image #72

brunosoaresds opened this issue Jan 15, 2019 · 10 comments

Comments

@brunosoaresds
Copy link

brunosoaresds commented Jan 15, 2019

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
Copy link

Same problem here!!

@T0shik
Copy link

T0shik commented Feb 3, 2019

Same issue.

@lfreneda
Copy link

lfreneda commented Feb 5, 2019

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

@brunosoaresds
Copy link
Author

brunosoaresds commented Feb 5, 2019

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
Copy link

draco1989 commented Feb 5, 2019

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
Copy link
Author

@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
Copy link

lfreneda commented Feb 5, 2019

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! 👯‍♀️

@lfreneda
Copy link

lfreneda commented Feb 5, 2019

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

image

@draco1989
Copy link

@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
Copy link

allanpoppe commented May 9, 2019

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants