Android developmentAndroid tutorial

What is android.os.FileUriExposedException

Hii Everyone in This Article I am sharing how to resolve Android Camera App Creasing in  Android 9. If you share files with other apps using a Uri, you may have encountered this error on API 24+. Error is android.os.FileUriExposedException. And hare I Give you solution What is android.os.FileUriExposedException and what you can do about it.

 android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()

This error occurs when you try to used camera to click image and upload image to server with camera and gallery. android.os.FileUriExposedException. For one thing, we assume that the destination app has READ_EXTERNAL_PERMISSION which may not be the case.  As of Android N, in order to work around this issue, you need to use the FileProvider API.

Besides the solution using the- FileProvider, there is another way to work around this. Simply put

in Application.onCreate(). In this way, the VM ignores the file URI  exposure. Just paste the below code in Activity onCreate().

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); 
StrictMode.setVmPolicy(builder.build());

It will ignore URI exposure. Happy coding 🙂

Solutions 2: Any Other Solution ( android.os.FileUriExposedException )

Step 1: Manifest File.

<manifest ...>
    <application ...>
        <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>
    </application>
</manifest>

Step 2: Create XML file 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>

Step 3: Code changes

File file = ...;
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
// Old Approach
    install.setDataAndType(Uri.fromFile(file), mimeType);
// End Old approach
// New Approach
    Uri apkURI = FileProvider.getUriForFile(
                             context, 
                             context.getApplicationContext()
                             .getPackageName() + ".provider", file);
    install.setDataAndType(apkURI, mimeType);
    install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
// End New Approach
    context.startActivity(install);

Android Image UpLoad to server From Camera and gallery