1

I've been trying for days to get Shared Intent working on Android with an image, text and a link at the bottom. In the image is what I want to achieve. You can see what I get at the bottom.

What I want to achieve

This is the code I have right now:

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("<img src='http://example.com/images/image.jpg'><br />This is the text shared.<br />http://example.com"));

// shareIntent.putExtra(Intent.EXTRA_STREAM, DownloadImage("http://example.com/images/image.jpg"));
// shareIntent.putExtra(Intent.EXTRA_STREAM, Html.fromHtml("<img src='http://example.com/images/image.jpg'>"));

shareIntent.setType("*/*");
// shareIntent.setType("image/jpeg");
// shareIntent.setType("text/html");

shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share some data"));

What am I doing wrong? I have commented some things I have tried. Thanks for your help!

I have tried multiple data types and different types of image loading. Loading from url doesn't work and inserting a bitmap doesn't work. (DownloadImage returns a Bitmap and I have checked that it has no errors) I've replaced the url.

2 Answers2

1

Well I have fixed by watching some youtube videos.

Add this to your AndroidManifest whithin the application tag:

<provider
    android:name="androidx.core.content.FileProvider"
    android:authorities="your.package.file-provider"
    android:grantUriPermissions="true"
    android:exported="false">
    <meta-data
         android:name="android.support.FILE_PROVIDER_PATHS"
         android:resource="@xml/file_paths"/>
</provider>

Create an xml folder in res containing:

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

Then in the activity:

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml("Some text.<br />https://maybe.a.deeplink.in.your.app"));
shareIntent.putExtra(Intent.EXTRA_STREAM, getContentUri());
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share this with"));

Finally the function to get your image from ImageView you want (in my case this is image):

private Uri getContentUri() {
    Bitmap bitmap = null;
    BitmapDrawable bitmapDrawable = (BitmapDrawable) image.getDrawable();
    bitmap = bitmapDrawable.getBitmap();

    File imagesFolder = new File(getCacheDir(), "images");
    Uri contentUri = null;
    try {
        imagesFolder.mkdirs();
        File file = new File(imagesFolder, "shared_image.png");
        FileOutputStream stream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        stream.flush();
        stream.close();
        contentUri = FileProvider.getUriForFile(this, "your.package.file-provider", file);
    } catch (Exception e) {
         Toast("Error => " + e.getMessage());
    }
        return contentUri;
}

Now your image will be saved as shared_image.png and you have a uri you can work with!

Goodluck :)

-3

https://guides.codepath.com/android/Displaying-Images-with-the-Glide-Library

Check this link this helps to load url and transform url into image

I hope this will work in your case.