Updated 25 August 2017
As I was working with the camera app trying to get the image URI from it. My app crashed and I was not able to open the camera. This was happening in the devices which as API level greater than 24. The code works fine for lesser API levels.
The actual problem was the way in which I was trying to manage files from one app (the main app) to the other (the camera app, which is a server app). The old file Uri scheme is banned for the apps with targetSDKVersion of 24 and higher.
The solution to this issue is that the developer should use a File Provider in order to manage files from one app to another. You can follow the below steps to resolve this issue.
1) Add a provider in the “AndroidManifest.xml” file
1 2 3 4 5 6 7 8 9 |
<provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.my.package.name.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths" /> </provider> |
2) Define a “provider_paths.xml” file under your “xml” folder in “res” folder.
1 2 3 4 5 |
<paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="my_images" path="."/> </paths> |
3) And in your activity you can replace
1 |
mFileUri = Uri.fromFile(new File(imagesFolder, "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg")); |
to
1 |
mFileUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".my.package.name.provider", new File(imagesFolder, "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg")); |
Or you can use both as
1 2 3 4 5 |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { mFileUri = FileProvider.getUriForFile(this, getApplicationContext().getPackageName() + ".my.package.name.provider", new File(imagesFolder, "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg")); } else { mFileUri = Uri.fromFile(new File(imagesFolder, "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg")); } |
That’s all !!! Just follow these simple steps and you are good to go.
Thank you very much. This is Vedesh Kumar signing off.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.