Android Share details via only selected social media Apps - android

I want to share message with only whatsApp ,viber,twitter and gmail apps so write my code as following using Intent.createChooser but it doesn't work, even doesn't show error I am calling this method inside fragment...this code worked in my another project there i called this method inside dialog..
public void shareDetails(String message) {
List<Intent> intentShareList = new ArrayList<Intent>();
Intent shareIntent = new Intent();
List<ResolveInfo> resolveInfoList = context.getPackageManager().queryIntentActivities(shareIntent, 0);
try {
for (ResolveInfo resInfo : resolveInfoList) {
String packageName = resInfo.activityInfo.packageName;
String name = resInfo.activityInfo.name;
if (packageName.contains("om.viber.voip") ||
packageName.contains("com.twitter.android") ||
packageName.contains("com.google.android.apps.plus") ||
packageName.contains("com.google.android.gm") ||
packageName.contains("com.whatsapp")) {
if (name.contains("com.twitter.android.DMActivity")) {
continue;
}
shareIntent.setComponent(new ComponentName(packageName, name));
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, message);
intentShareList.add(shareIntent);
}
}
if (intentShareList.isEmpty()) {
Toast.makeText(context, "No apps to share !", Toast.LENGTH_SHORT).show();
} else {
Intent chooserIntent = Intent.createChooser(intentShareList.remove(0), "Share Product Details via");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentShareList.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}
} catch (Exception e) {
}
}
please help

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"));

Your have used Parcelable with no size. Try this modified one.
public void shareDetails(String message) {
try {
List<Intent> intentShareList = new ArrayList<Intent>();
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
List<ResolveInfo> resolveInfoList = getPackageManager().queryIntentActivities(shareIntent, 0);
if (!resolveInfoList.isEmpty()) {
for (ResolveInfo resolveInfo : resolveInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
targetedShareIntent.setType("text/plain");
targetedShareIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);
if (packageName.contains("om.viber.voip") ||
packageName.contains("com.twitter.android") ||
packageName.contains("com.google.android.apps.plus") ||
packageName.contains("com.google.android.gm") ||
packageName.contains("com.whatsapp")) {
targetedShareIntent.setPackage(packageName);
intentShareList.add(targetedShareIntent);
}
}
if (intentShareList.isEmpty()) {
Toast.makeText(context, "No apps to share !", Toast.LENGTH_SHORT).show();
} else {
Intent chooserIntent = Intent.createChooser(intentShareList.remove(0), "Share Product Details via");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentShareList.toArray(new Parcelable[intentShareList.size()]));
startActivity(chooserIntent);
}
}
} catch (Exception e) {
}
}

Related

how to capture image from doc scanner app and show in imageview in android?

I am trying to capture image in android phone, but before capturing i wish to show user by which app to proceed further. My goal is to give user the list of apps which can click photo, but currently only camera photo is showing. Please guide me resolving this issue.
I am trying to capture image from doc scanner and then upload it from my app.
public Intent getPickImageChooserIntent() {
Uri outputFileUri = getCaptureImageOutputUri();
List<Intent> allIntents = new ArrayList<>();
PackageManager packageManager = getPackageManager();
try {
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent , 0);
for (ResolveInfo res : listCam) {
Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName,
res.activityInfo.name));
intent.setPackage(res.activityInfo.packageName);
if (outputFileUri != null) {
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
}
allIntents.add(intent);
}
} catch (Exception e) {
e.printStackTrace();
}
Intent mainIntent = allIntents.get(allIntents.size() - 1);
for (Intent intent : allIntents) {
if (intent.getComponent().getClassName().equals("com.android.documentsui.DocumentsActivity")) {
mainIntent = intent;
break;
}
}
allIntents.remove(mainIntent);
Intent chooserIntent = Intent.createChooser(mainIntent, "Select source");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, allIntents.toArray(new
Parcelable[allIntents.size()]));
return chooserIntent;
}
public Uri getCaptureImageOutputUri() {
Uri outputFileUri = null;
try {
File getImage = getExternalFilesDir("");
if (getImage != null) {
outputFileUri = Uri.fromFile(new File(getImage.getPath(), "profile.png"));
}
} catch (Exception e) {
e.printStackTrace();
}
return outputFileUri;
}
myImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (checkCameraPermission()) {
startActivityForResult(getPickImageChooserIntent(), 1);
}
}
});
Calling code:
startActivityForResult(getPickImageChooserIntent(), 1);

Cannot Create or Read files from storage when App launched from another

I'm opening my Application A from Application B using below code.
public void openApp(Context context, String appName, String packageName) {
if (isAppInstalled(context, packageName)) {
if (isAppEnabled(context, packageName)) {
Intent intent = new Intent();
intent.setAction(AppConst.ACTION_CONSTANT);
intent.putExtra(Intent.EXTRA_TEXT, "Init from Communicator");
intent.setType("text/plain");
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Toast.makeText(context, getString(R.string.install_latest_app, appName), Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(context, getString(R.string.app_not_enabled, appName), Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(context, getString(R.string.app_not_installed, appName), Toast.LENGTH_SHORT).show();
}
}
Now the problem is, i'm getting the URI of selected Image file from Gallery using
private void openGallery() {
Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
getIntent.setType("image/*");
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
Intent chooserIntent = Intent.createChooser(getIntent, "Select Image");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{pickIntent});
startActivityForResult(chooserIntent, PICK_IMAGE);
}
The resultant URI I'm passing to server using ValueCallback<Uri[]> filePathCallback by setting
mUMA.onReceiveValue(new Uri[]{capturedImageUri});
This all is working fine when i'm opening the application, but failing when opening from Application B.
Do i need to set any extra permission settings when opening from Application B ?

How to Share Image And Text to Facebook and Twitter From Android

I am developing one application in that I want to share Image and some text. Both are send to twitter but while I share to facebook image only share but not shared text.
My code is:
public void open(View view) {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse("file:///sdcard/photo.jpg");
sharingIntent.setType("image/*");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Body text of the new status");
sharingIntent.putExtra(Intent.EXTRA_TITLE, "Traffic At");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
}
public class Share extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent i=getIntent();
String data=i.getStringExtra("data");
String shareType=i.getStringExtra("type");
if(shareType.equals("text"))
{
sharetext(data);
}
else if(shareType.equals("image"))
{
shareimage(data);
}
}
public void sharetext(String text) //Text to be shared
{
Intent share=new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(android.content.Intent.EXTRA_SUBJECT,"TITLE");
share.putExtra(android.content.Intent.EXTRA_TEXT,text);
startActivity(Intent.createChooser(share,"Share via"));
finish();
}
public void shareimage(String text) //argument is image file name with extention
{
Intent shareimage=new Intent(android.content.Intent.ACTION_SEND);
shareimage.setType("*/*");//for all
shareimage.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
shareimage.putExtra(Intent.EXTRA_STREAM,Uri.parse("file://"+text));
startActivity(Intent.createChooser(shareimage, "Share Image"));
finish();
}
}
call this activity
with bundle containing values
StringExtra("data"); //file name or text to share
StringExtra("type"); // "text" for sharing text, "image" for sharing image
Try to use the following method.
void share(String appName, String title, String text, String imagePath) {
try {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/*");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);
targetedShare.setType("image/*"); // put here your mime type
if (info.activityInfo.packageName.toLowerCase().contains(appName) || info.activityInfo.name.toLowerCase().contains(appName)) {
targetedShare.putExtra(Intent.EXTRA_SUBJECT, title);
targetedShare.putExtra(Intent.EXTRA_TEXT, text);
targetedShare.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imagePath)));
targetedShare.setPackage(info.activityInfo.packageName);
targetedShareIntents.add(targetedShare);
}
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}
} catch (Exception e) {
Log.v("VM", "Exception while sending image on" + nameApp + " " + e.getMessage());
}
}
To share via facebook call share("facebook", title, text, imagePath), and share("twitter", title, text, imagePath) for twitter.

java null pointer exception when clicked on ImageView

Iam setting a share button for twitter to share a pic from my drawable later to be changed dynamically i got this code from the net and paste it in my app i got the following error null pointer exception and cant figure it out any help would be appreciated thank you.
enter code here
ImageView twittersharing =(ImageView)findViewById(R.id.twitter_sharing);
twittersharing.setOnClickListener(new View.OnClickListener()
{
private Context context;
#Override
public void onClick(View arg0)
{
//Toast.makeText(getBaseContext(), "On click", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
File filePath = context.getFileStreamPath("placeholder.jpg");
share("com.twitter.android",filePath.toString());
}
});
public void share(String nameApp, String imagePath)
{
try
{
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpg");
List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
if (!resInfo.isEmpty())
{
for (ResolveInfo info : resInfo)
{
Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);
targetedShare.setType("image/jpg"); // put here your mime type
if (info.activityInfo.packageName.toLowerCase().contains(nameApp) || info.activityInfo.name.toLowerCase().contains(nameApp)) {
targetedShare.putExtra(Intent.EXTRA_SUBJECT, "Sample Photo");
targetedShare.putExtra(Intent.EXTRA_TEXT,"This photo is created by ME");
targetedShare.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imagePath)) );
targetedShare.setPackage(info.activityInfo.packageName);
targetedShareIntents.add(targetedShare);
}
}
Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}
}
catch(Exception e)
{
Log.v("VM","Exception while sending image on" + nameApp + " "+ e.getMessage());
}
}
Try changing the picture using:
twitterSharing.setImageDrawable(R.drawable.placeholder);
Can you tell where exactly it is giving NullPoitnerException?
Also, you do have that image mentioned here (placeholder.jpg) in your device, right?
File filePath = context.getFileStreamPath("placeholder.jpg");
share("com.twitter.android",filePath.toString());

How to use whatsapp from my Android app?

This is how I am calling the SMS app:
Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "The SMS text");
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);
How do I do the same for sending messages via twitter/Whatsapp/Facebook? What should I write in place of mms-sms? I found no documentation on such.
I can't also find any way of calling Facebook/Twitter directly, but you could always call android.content.Intent.ACTION_SEND and let the user choose the application.
Intent i = new Intent(android.content.Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
i.putExtra(Intent.EXTRA_TEXT, "Message body");
startActivity(Intent.createChooser(i, "Share dialog title"));
However, there might be a bug when using this to share through Facebook. For more information please see: Android Facebook Intent
public void onClickWhatsApp(View view) {
try {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "YOUR TEXT HERE";
waIntent.setPackage("com.whatsapp");
if (waIntent != null) {
waIntent.putExtra(Intent.EXTRA_TEXT, text);//
startActivity(Intent.createChooser(waIntent, "Share with"));
} else {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}} catch (NameNotFoundException e) {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
}
Intent i = new Intent(Intent.ACTION_SEND);
i.setPackage("com.whatsapp");
i.putExtra("chat",true);
i.setType("text/plain");
startActivity(i);
You can use the following snippets:
For WhatsApp:
public void onClickWhatsApp(View view) {
PackageManager pm=getPackageManager();
try {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "YOUR TEXT HERE";
PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
//Check if package exists or not. If not then code
//in catch block will be called
waIntent.setPackage("com.whatsapp");
waIntent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(waIntent, "Share with"));
} catch (NameNotFoundException e) {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
}
For Twitter:
void shareOnTwitter()
{
PackageManager pm=getPackageManager();
try {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "Insert Tweet Here";
#SuppressWarnings("unused")
PackageInfo info=pm.getPackageInfo("com.twitter.android", PackageManager.GET_META_DATA);
//Check if package exists or not. If not then code
//in catch block will be called
waIntent.setPackage("com.twitter.android");
waIntent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(waIntent, "Share with"));
} catch (NameNotFoundException e) {
Toast.makeText(this, "Twitter not Installed", Toast.LENGTH_SHORT)
.show();
return ;
}
return ;
}

Categories

Resources