Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exposed beyond app through ClipData.Item.getUri

Tags:

java

android

For sdk 24 and up, if you need to get the Uri of a file outside your app storage you have this error.
@eranda.del solutions let you change the policy to allow this and it works fine.

However if you want to follow the google guideline without having to change the API policy of your app you have to use a FileProvider.

At first to get the URI of a file you need to use FileProvider.getUriForFile() method:

Uri imageUri = FileProvider.getUriForFile(
            MainActivity.this,
            "com.example.homefolder.example.provider", //(use your app signature + ".provider" )
            imageFile);

Then you need to configure your provider in your android manifest :

<application>
  ...
     <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.example.homefolder.example.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <!-- ressource file to create -->
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths">  
        </meta-data>
    </provider>
</application>

(In "authorities" use the same value than the second argument of the getUriForFile() method (app signature + ".provider"))

And finally you need to create the ressources file: "file_paths". This file need to be created under the res/xml directory (you probably need to create this directory too) :

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="." />
</paths>

Add following code block before start camera or file browsing

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

Please refer the referral link strict mode and its explained all the usage and technical details.


No required provider configuration into AndroidManifest only Camera and Storage permission should be permitted. Use the following code to start the camera:

final int REQUEST_ACTION_CAMERA = 9;
void openCamra() {
Intent cameraImgIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

cameraImgIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID +".provider",
                new File("your_file_name_with_dir")));
startActivityForResult(cameraImgIntent, REQUEST_ACTION_CAMERA);
}

After capturing the image, you can find your captured image in:

"your_file_name_with_dir"