ImagePicker icon indicating copy to clipboard operation
ImagePicker copied to clipboard

IllegalArgumentException in getUriForFile on Huawei Android 7 devices

Open chrisaut opened this issue 6 years ago • 5 comments

I'm seeing crash reports as follows:

java.lang.IllegalArgumentException: 
  at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile (FileProvider.java:711)
  at android.support.v4.content.FileProvider.getUriForFile (FileProvider.java:400)
  at com.linchaolong.android.imagepicker.Utils.getContentUri (Utils.java:31)
  at com.linchaolong.android.imagepicker.Utils.getIntentUri (Utils.java:24)
  at com.linchaolong.android.imagepicker.cropper.CropImage.getCameraIntents (CropImage.java:240)
  at com.linchaolong.android.imagepicker.cropper.CropImage.getPickImageChooserIntent (CropImage.java:177
at com.linchaolong.android.imagepicker.ImagePicker.startChooser (ImagePicker.java:71)

They all seem to come from Huawei devices running Android 7.

I found this stackoverflow thread discussing the issue https://stackoverflow.com/questions/39895579/fileprovider-error-onhuawei-devices but frankly I don't know if it's possible to implement these workarounds from outside the picker code itself, or if this needs to be done as part of the picker?

Any advice?

chrisaut avatar Aug 16 '17 02:08 chrisaut

@chrisaut I without huawei andriod n device, did you fix it? I think it's better to be part of the picker

CYRUS-STUDIO avatar Aug 17 '17 14:08 CYRUS-STUDIO

Is this bug fixed?

Currently I had the same problem in my own app. This was how I fixed it:

Because FileProvider.getUriForFile(...) uses internally ContextCompat.getExternalFilesDirs(..). Now, when we use Context.getExternalFilesDir(..) it will crash on some Huawei devices. This is because Huawei devices and the Android documentation of Context.getExternalFilesDir(..) are inconsistent. (more here: https://stackoverflow.com/questions/39895579/fileprovider-error-onhuawei-devices).

I just take care I am going to use also ContextCompat.getexternalFilesDirs(..) instead of Context.getExternalFilesDir(..). This way, everything works fine for me.

public static File getExternalFilesDir(Context context) {
        // IMPORTANT TO USE THIS METHOD, INSTEAD OF Context.getExternalFilesDir(..)!!!!
	// There is a bug on Huawei-Devices, which select the wrong external path.
	// More info here: https://stackoverflow.com/questions/39895579/fileprovider-error-onhuawei-devices
	File[] externalFilesDirs = ContextCompat.getExternalFilesDirs(context, null);
	if (externalFilesDirs.length > 0) {
		return externalFilesDirs[0];
	} else {
		return null;
	}
}

ghost avatar Feb 22 '18 13:02 ghost

@hubermichael88 Hi, where did u implement this solution ? Can you please give a more detailed example? Thank you!

jppavez avatar Feb 28 '18 18:02 jppavez

It is more a workaround, better would be to fix it within the picker. Just make sure you NEVER call "Context.getExternalFilesDir(..) in your own app-code, use instead the method above which makes sure to get the right external path.

ghost avatar Mar 01 '18 07:03 ghost

@jppavez you can new a custom provider

fjg2016 avatar Nov 06 '18 08:11 fjg2016