Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intent.ACTION_SEND not working on Oreo [duplicate]

I am developing a custom camera application which captures a picture and stores it in the gallery. When I share that image using Intent.ACTION_SEND, it works absolutely fine on all devices except for devices having API 26, i.e. OREO.

My code to share the image is:

Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.setType("image/jpeg");
            Uri uriShare = Uri.fromFile(outFile);
            //outfile is the path of the image stored in the gallery
            shareIntent.putExtra(Intent.EXTRA_STREAM, uriShare);
            startActivity(Intent.createChooser(shareIntent, ""));

Can anyone help me resolve this issue?

like image 727
Ali Haider Avatar asked Nov 28 '22 04:11

Ali Haider


2 Answers

If targetSdkVersion is higher than 24, then FileProvider is used to grant access.

Create an xml file(Path: res\xml) provider_paths.xml

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

Add a Provider in AndroidManifest.xml under

 <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>

and replace

Uri uri = Uri.fromFile(fileImagePath);

to

Uri uri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID + ".provider",fileImagePath);

and you are done.

like image 103
Ankit Patidar Avatar answered Dec 05 '22 18:12

Ankit Patidar


Use File Provider for getting IMAGE URI and add flags to the intent... follow these steps carefully.. android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData() This update has been done since nougat..

like image 41
Santanu Sur Avatar answered Dec 05 '22 18:12

Santanu Sur