CYRUS-STUDIO / ImagePicker

Android下的图片选择与裁剪开源库(android image picker and cropper library)
http://www.jianshu.com/p/35ce3b82773e
239 stars 68 forks source link

IllegalArgumentException in getUriForFile on Huawei Android 7 devices #10

Open chrisaut opened 7 years ago

chrisaut commented 7 years ago

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?

CYRUS-STUDIO commented 7 years ago

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

ghost commented 6 years ago

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;
    }
}
jppavez commented 6 years ago

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

ghost commented 6 years ago

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.

fjg2016 commented 5 years ago

@jppavez you can new a custom provider