Introduction:
In this article, we will learn How to handle “android.os.FileUriExposedException”, if we have an app that shares files with other apps using an Uri on API 24+. As of Android N, we need to use FileProvider API
Creating New Project with Android Studio
- Open Android Studio and Select Create new project.
- Name the project as your wish and select your activity template.
- Click “finish” button to create new project in Android Studio.
Steps to Change File Provider API
To replace “file://” to “uri://”, We should follow the three steps.Step 1: Change Manifest Entry
- Add <provider /> tag with FileProvider inside the
tag as shown in the below code. <provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/> </provider>
- Here, provider_paths is a xml file which is used to specify the path to be accessed via File Provider API for Android N & above devices.
- Don't forget to add the following permission in your manifest file
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
- Create a new xml folder in res folder. Then create xml file and named as provider_paths.xml
- Add the following code to the file.
<?xml version="1.0" encoding="utf-8"?> <paths> <external-path name="external_files" path="."/> </paths>
- Change the normal Uri method for Android N.
ToFile file = new File("File Path"); Uri.fromFile(file)
File file = new File("File Path"); Uri apkURI = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".provider", file);
- Then Grant Read Uri Permission for Android N & above devices. The following code shows how to use Open file Intent for Android N devices & before Android N devices.
File file = new File("File Path"); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { Uri apkURI = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".provider", file); intent.setDataAndType(apkURI, "image/jpg"); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); } else { intent.setDataAndType(Uri.fromFile(file), "image/jpg"); } startActivity(intent);
Great Article
ReplyDeleteandroid based projects
Java Training in Chennai
Project Center in Chennai
Java Training in Chennai
projects for cse
The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training
Serves24 is Best Air conditioner repair In Hyderabad We Provided Door Step Repair And Service & Provide best in class. Get 6 months repair guarantee. Book Now just @249/-. at most affordable prices
ReplyDeleteair conditioner repair services
air conditioner repairs services hyderabad
air conditioner repair hyderabad
air conditioner repair services
Really Very Good Article