Skip to content Skip to sidebar Skip to footer

Sharing On Twitter And Facebook Via Android App

I'm developing an adroid app with wich I can share text (and later pictures too) via facebook and twitter. I found some code which is opening the facebook/twitter sharing window bu

Solution 1:

Use this method to share photo with text. Call this method and pass argument nameofapp and imagepath. Name of app means like on which you want to share like gmail , facebook , twitter.

privatevoidshare(String nameApp, String imagePath,String text) {
try
{
    List<Intent> targetedShareIntents = newArrayList<Intent>();
    Intent share = newIntent(android.content.Intent.ACTION_SEND);
    share.setType("image/jpeg");
    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
    if (!resInfo.isEmpty()){
        for (ResolveInfo info : resInfo) {
                Intent targetedShare = newIntent(android.content.Intent.ACTION_SEND);
                targetedShare.setType("image/jpeg"); // put here your mime typeif (info.activityInfo.packageName.toLowerCase().contains(nameApp) || info.activityInfo.name.toLowerCase().contains(nameApp)) {
                    targetedShare.putExtra(Intent.EXTRA_SUBJECT, text);
                    targetedShare.putExtra(Intent.EXTRA_TEXT,text);
                    targetedShare.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(newFile(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(newParcelable[]{}));
            startActivity(chooserIntent);
    }
}
catch(Exception e){
}
}

Use this for facebook :-

try {
File filePath = "/mnt/sdcard/vmphoto.jpg"; //This is imagefile path in your change it acc. to your requirement.
share("facebook",filePath.toString(),"Hello"); <<<<<Hello is text. send acc. to you req.

}
catch(Exception e) {
       //exception occur might your app like gmail , facebook etc not installed or not working correctly.
}

For twitter

try {
File filePath = "/mnt/sdcard/vmphoto.jpg"; //This is imagefile path in your change it acc. to your requirement.
share("twitter",filePath.toString(),"Hello"); <<<<<Hello is a text send acc. to you req.

}
catch(Exception e) {
       //exception occur might your app like gmail , facebook etc not installed or not working correctly.
}

Solution 2:

For attaching image on gmail,facebook,twitter with text use below code.

FilefilePath= context.getFileStreamPath("vmphoto.jpg");
share("gmail",filePath.toString());
share("facebook",filePath.toString());
share("twitter",filePath.toString());

// code of share(String nameApp,String imagePath) function

voidshare(String nameApp, String imagePath) {
 try
{
  List<Intent> targetedShareIntents = newArrayList<Intent>();
  Intentshare=newIntent(android.content.Intent.ACTION_SEND);
    share.setType("image/jpeg");
  List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
  if (!resInfo.isEmpty()){
    for (ResolveInfo info : resInfo) {
        IntenttargetedShare=newIntent(android.content.Intent.ACTION_SEND);
        targetedShare.setType("image/jpeg"); // put here your mime typeif (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 App Name");
            targetedShare.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(newFile(imagePath)) );
            targetedShare.setPackage(info.activityInfo.packageName);
            targetedShareIntents.add(targetedShare);
        }
    }
    IntentchooserIntent= Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(newParcelable[]{}));
    startActivity(chooserIntent);
  }
  }

       catch(Exception e){
      Log.v("VM","Exception while sending image on" + nameApp + " "+  e.getMessage());
    }
  }

Post a Comment for "Sharing On Twitter And Facebook Via Android App"