first, write in front
Recently, because Lio Zilao busy with his company's Things and Okgo (a focus on making the network request simpler Network framework), so that LZ replacement maintenance Imagepicker (a support single, multi-select, rotate and crop image selector), but also deal with a lot of bugs, and recently finally stabilized, This is where you can share the camera adaptation scheme with Android N (API 24).
Android Nougat has been updated for a long time, as a andorid developer, we have the obligation to prepare ourselves to adjust the targetsdkversion as the latest one, thus directly increased from the previous 23 to 25.
As always, whenever we adjust the targetsdkversion, we need to check that each part of our code works very well. If you simply change the code, I can say that your application is at high risk of crashing or failing. In this case, when you change your app's Targetsdkversion 24, we need to check every feature perfect work on Android Nougat (24) above.
After I got the 7.0 Xiaomi 5 test machine, I couldn't wait to test the imagepicker of my maintenance, but indeed, like everyone else's issuse, it crashed directly when the system camera was called.
second, exactly what triggered the 7.0 camera crash
Follow up the error log to source discovery, crash when we call the camera to get the Uri.
The reason is obvious that file://is not allowed as an additional Uri intent, otherwise it will throw fileuriexposedexception.
third, why under the Android Nougat file://not be allowed?
You might be wondering why the Android team decided to change this behavior.
There is a good reason behind this, if the file path is sent to the target application (the camera application in this case), the file will be fully accessible through the camera application process, and not just the initiator can receive.
But let's think about it, actually it's up to our app to launch the camera to take a photo and save the representative file as our app. Therefore, the access to the file should be our application and not the camera application itself. That's why file://now requires every developer to do this task in Targetsdkversion 24.
Iv. How to solve the problem?
Since file://is no longer allowed, what should we do with it? The answer is to solve it through fileprovider.
How should we get fileprovider to solve it?
1, the first is in the Androidmanifest.xml affirmed
<provider
Android:authorities= "${applicationid}.provider"
Android:name= ". Imagepickerprovider "
Android:exported= "false"
Android:granturipermissions= "true" >
<meta-data
Android:name= "Android.support.FILE_PROVIDER_PATHS"
Android:resource= "@xml/provider_paths"/>
</provider>
2. Create a Provider_paths.xml file under the XML folder under the Res folder.
Res/xml/provider_paths.xml
<?xml version= "1.0" encoding= "Utf-8"? ><paths xmlns:android= "Http://schemas.android.com/apk/res/android" > <external-path name= "External_files" path= "." /></paths>
3, in the appropriate place to replace it
URI Uri; if(VERSION. Sdk_int <=Version_codes. M) {URI=Uri.fromfile (Takeimagefile); }Else{ /*** 7.0 Call system camera no longer allow URI mode, should be replaced with fileprovider * and this will resolve MIUI system on the photo return size 0 case */URI=fileprovider.geturiforfile (activity, providerutil.getfileprovidername (activity), takeimagefile); } LOG.E ("Nanchen", Providerutil.getfileprovidername (activity)); Takepictureintent.putextra (Mediastore.extra_output,uri);Five, a few notes
For the three steps above, make a few notes:
1. The Name property of the provider in the Androidmanifest.xml file declares why. Magepickerprovider (in fact, this is a class that inherits from Fileprovider but does not implement) and does not directly assign name to Android.support.v4.content.FileProvider?
This is because Imagepicker as a picture selection framework, and your App may also have a statement, in order to avoid Android Studio at the time of compilation of the merge Module led to conflict, here is the insurance to declare a different name.
2. Why the Authority attribute declared in the Androidmanifest.xml file is ${applicationid}.provider, not a fixed name.
This is because in Android, requires authority must be unique, if you define a provider for it to specify a unique authority, here and take imagepicker for example, if you use the Imagepic in an App Ker as a picture selection frame, and when you use Imagepicker again in another app, the system checks if the authority of the installed app is the same as the authority you want to install the app, and if the same will pop up the following warning and the installation fails.
So we use the form of ApplicationID + provider when we define authorities, and we can get it by the package name + provider when we get the authorities. The code is as follows:
Package Com.lzy.imagepicker.util; Import Android.content.Context; /** * Used to resolve provider conflict Util * * author:nanchen * Email: [Email protected] * Date:2017-03-22 */ Public class Providerutil { publicstatic String Getfileprovidername (context context) { return context.getpackagename () + ". Provider"; }}
six, written in the last
The above will solve the Android N camera crashes, such as the wrong place to write, you are welcome to comment in the comments area message.
Picture frame Imagepicker Address: https://github.com/jeasonlzy/ImagePicker
The article was posted to Jane book in sync: http://www.jianshu.com/p/57ba0d1511b9
"Adaptation finishing" Android 7.0 system camera crash resolution Android.os.FileUriExposedException