Android - Share on Facebook, Twitter, Mail, Ecc

Android - Share on Facebook, Twitter, Mail, ecc

Paresh Mayani's answer is mostly correct. Simply use a Broadcast Intent to let the system and all the other apps choose in what way the content is going to be shared.

To share text use the following code:

String message = "Text I want to share.";
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT, message);

startActivity(Intent.createChooser(share, "Title of the dialog the system will open"));

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.

How to share web application with facebook, twitter etc

Try this :

findViewById(R.id.btnEnvoyer).setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
Intent sendMailIntent = new Intent(Intent.ACTION_SEND);
sendMailIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.Share_Mail_Subject));
sendMailIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.Share_Mail_Text));
sendMailIntent.setType("text/plain");

startActivity(Intent.createChooser(sendMailIntent, "Email / SMS / Tweet ?"));
}
}
);

works just fine in my latest app if you want to test it :
Comis Strips in Brussels

=> Hit [Menu], then [Share], then the button [Send Email / SMS / Tweet]=R.id.btnEnvoyer in my layout file to see the alternatives the user can choose from ...

Hope this helps you a bit ...

H.

How to share text in twitter,facebook and email in android?

use this and let user choose where he/she wants to share it

Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, "Hello");
i.putExtra(Intent.EXTRA_TEXT, "I am sharing this");
try
{
this.startActivity(Intent.createChooser(i, "Share..."));
}
catch (android.content.ActivityNotFoundException ex)
{
//Error
return;
}

How to share information links on twitter, gmail, facebook etc

Take a look here, here and here. It looks like you need ACTION_SEND intent.



Related Topics



Leave a reply



Submit