0

I am using this code to share the image on Whatsapp, Facebook, and Instagram etc. This code works fine below API 25 but Not Working above API 25.

Intent share = new Intent("android.intent.action.SEND");
                share.setType("image/jpeg");
                share.putExtra("android.intent.extra.STREAM", Uri.parse(this.imgUrl));
                startActivity(Intent.createChooser(share, "via"));
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

4 Answers4

1

First make sure you have added permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Then use this code

Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
Uri imageUri =  Uri.parse(this.imgUrl);                                
share.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(share, "Select"));
Anisuzzaman Babla
  • 6,510
  • 7
  • 36
  • 53
1

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

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

now Add a Provider in AndroidManifest.xml file

<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 this with your code

Intent share = new Intent("android.intent.action.SEND");
            share.setType("image/jpeg");
Uri uri = FileProvider.getUriForFile(MainActivity.this, BuildConfig.APPLICATION_ID+ ".provider",new File(this.imgUrl));
            share.putExtra("android.intent.extra.STREAM", uri);

            startActivity(Intent.createChooser(share, "via"));

Update

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

Try this:

Intent shareIntent = new Intent();
     shareIntent.setAction(Intent.ACTION_SEND); 
     shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage); 
     shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent,getResources().getText(R.string.send_to)));
Gourango Sutradhar
  • 1,461
  • 10
  • 16
0

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