user3917631
user3917631

Reputation: 139

Share a link with a thumbnail in android

I'm trying to implement a share that will send a dynamic link, a title and an image. As a start I want it to work with WhatsApp. I implemented the share intent as per the documentation:

                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.setType("text/plain");
                sendIntent.putExtra(Intent.EXTRA_TEXT, dynamicLink);
                sendIntent.putExtra(Intent.EXTRA_SUBJECT, "Title");

                // If item has images, pick the first one as thumbnail
                if(!item.getImagesBitmaps().isEmpty()){
                    Uri imageUri = ...(Getting image uri)
                    sendIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
                    sendIntent.setType("image/jpg");
                }

                sendIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                Intent shareIntent = Intent.createChooser(sendIntent, null);
                startActivity(shareIntent);

This code works. I can send the link to WhatsApp with the image and it looks as a standard image-message:

enter image description here

However my goal is to have a link with a thumbnail, like this:

enter image description here

Meaning something with a clickable thumbnail and this sort of header with title and description.

I searched quite a lot for an answer on how it can be done, and while there are similar questions here with answers they seem either old, incomplete or something that I couldn't understand how to fit with the shareIntent implementation. I couldn't extract anything tangible from them that I could try.

Appreciate the help.

Upvotes: 1

Views: 1057

Answers (1)

Meet
Meet

Reputation: 895

If you are trying to get image from server URL then you have to load the image with Glide first(add the Glide dependency), then send the bitmap.

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/shareButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="share text and image "
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt:

package com.application.myApp

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.net.Uri
import androidx.annotation.Nullable
import androidx.core.content.FileProvider.getUriForFile
import com.bumptech.glide.request.target.CustomTarget
import java.io.File
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException

class MainActivity : AppCompatActivity() {
        private lateinit var sharebutton:Button
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            sharebutton = findViewById<Button>(R.id.shareButton)
            sharebutton.setOnClickListener {
               Glide.with(this)
                    .asBitmap()
                    .load(your_image_url)
                    .into(object : CustomTarget<Bitmap?>() {
                        override fun onLoadCleared(@Nullable placeholder: Drawable?) {}
                        override fun onResourceReady(
                            bitmap: Bitmap,
                            transition: com.bumptech.glide.request.transition.Transition<in Bitmap?>?
                        ) {
                            val cachePath = File(externalCacheDir, "my_images/")
                            cachePath.mkdirs()
                            val file = File(cachePath, "Image_123.png")
                            val fileOutputStream: FileOutputStream
                            try {
                                fileOutputStream = FileOutputStream(file)
                                bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)
                                fileOutputStream.flush()
                                fileOutputStream.close()
                            } catch (e: FileNotFoundException) {
                                e.printStackTrace()
                            } catch (e: IOException) {
                                e.printStackTrace()
                            }
                            val myImageFileUri: Uri =
                                getUriForFile(this@MainActivity, applicationContext.packageName + ".provider", file)
                            val intent = Intent(Intent.ACTION_SEND)
                            intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
                            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                            intent.putExtra(Intent.EXTRA_STREAM, myImageFileUri)
                            val sharelink = "www.google.com"
                            intent.putExtra(Intent.EXTRA_TEXT, sharelink)
                            intent.type = "image/*"
                            startActivity(Intent.createChooser(intent, null))
                        }
                    })
            }
        }
}

Create a folder named xml in res directory and put this file in that folder.

file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-cache-path name="external_files" path="my_images/"/>
</paths>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.application.myApp">

  <application
     .....>
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.file_provider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
  </application>
</manifest>

Upvotes: 1

Related Questions