Share Image and Text Through Whatsapp or Facebook

Share image and text through Whatsapp or Facebook

Please try the below code and hopefully it will work.

    Uri imgUri = Uri.parse(pictureFile.getAbsolutePath());
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, "The text you wanted to share");
whatsappIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
whatsappIntent.setType("image/jpeg");
whatsappIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

try {
activity.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
ToastHelper.MakeShortText("Whatsapp have not been installed.");
}

Share image and text through whatsapp

Whatsapp Support Image sharing along with text.

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT,title + "\n\nLink : " + link );
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(imageFilePath));
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share image via:"));

This will share image and EXTRA_TEXT will consider as image caption.

How do I share image and text together in WhatsApp

After doing a lot of trial and error, I found that the way we pass text via Intent.EXTRA_TEXT matters for WhatsApp!!

I just changed

intent.putExtra(Intent.EXTRA_TEXT, etShareMessage.text)

to

intent.putExtra(Intent.EXTRA_TEXT, etShareMessage.text.toString())

and it worked.

Directly Share an Image to Facebook, Twitter, Instagram, WhatsApp ect

I have solved my problem from my senior bro. I just added a line intent.setPackage(packageId) on my old share code. then it worked. The full process is given below.

Step 1.
Add these lines inside the application block of AndroidManifest.xml.

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

Step 2.
Create a file_path.xml file inside res/xml folder (if xml folder not available, create it). past this code in file_path.xml file.

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

<!--For Share-->
<external-path name="external_files" path="." />
<cache-path name="cache_files" path="/"/>
</paths>

Step 3.
Create a ShareUtils.kt object file and past this code.

package com.example.riz1.utils

import android.content.Context
import android.content.Intent
import android.net.Uri
import androidx.core.content.FileProvider
import com.liilab.photo_editor.BuildConfig
import java.io.File

object ShareUtils {
interface FileUriCreator { val uri: Uri }

fun shareImage(context: Context, imagePath: String, packageId: String){

if (packageId!=AppConstants.DEFAULT_PACKAGE_ID && context.packageManager.getLaunchIntentForPackage(packageId) == null) {
context.customToast("Please first install the app")
context.startActivity(Intent(Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=$packageId")
))
}else{
val fileUriCreator = object : FileUriCreator {
override val uri: Uri get() = FileProvider.getUriForFile(context,
BuildConfig.APPLICATION_ID + ".fileprovider", File(imagePath))
}
try {
val intent = Intent(Intent.ACTION_SEND)
if (packageId!=AppConstants.DEFAULT_PACKAGE_ID) intent.setPackage(packageId)
intent.type = "image/jpeg"
intent.putExtra(Intent.EXTRA_STREAM, fileUriCreator.uri)
context.startActivity(Intent.createChooser(intent, "Share Image"))
} catch (ex: Exception) {
context.customToast("Please first install the app")
context.startActivity(Intent(Intent.ACTION_VIEW,
Uri.parse("https://play.google.com/store/apps/details?id=$packageId")
))
}
}
}
}

Now just call ShareUtils.shareimage(context,ImagePath, packageId). If you want to open default share list just remove intent.setPackage(packageId) and can packageId would be null.

Share of Whatsapp and facebook along with text and image

Give this a try

Uri imageUri = Uri.parse(Filepath);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setPackage("com.whatsapp");
shareIntent.putExtra(Intent.EXTRA_TEXT, "My sample image text");
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

try {
startActivity(shareIntent);
} catch (android.content.ActivityNotFoundException ex) {
ToastHelper.MakeShortText("Kindly install whatsapp first");
}

PS: For sharing in multiple apps just create an alert dialogue and add
whtsapp,fb and other sharing option and while clicking on them you
have to call specific intent for this.As per my knowledge for sharing
in facebook you must impliment it's sdk, though facebook is able to
share simple image and hyperlink without integrating sdk.



Related Topics



Leave a reply



Submit