且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过Android应用程序分享在Twitter和Facebook

更新时间:2022-12-21 18:22:06

使用这种方法来共享照片与文字。调用此方法并传递参数nameofapp和的ImagePath。应用程序的名字的意思是一样的,你要共享,如Gmail,Facebook,Twitter的。

 私人无效份额(字符串nameApp,字符串的ImagePath,字符串文本){
尝试
{
    名单<意图> targetedShareIntents =新的ArrayList<意图>();
    意图份额=新的意图(android.content.Intent.ACTION_SEND);
    share.setType(为image / jpeg);
    名单< ResolveInfo> 。resInfo = getPackageManager()queryIntentActivities(份额,0);
    如果(!resInfo.isEmpty()){
        对于(ResolveInfo信息:resInfo){
                意图targetedShare =新的意图(android.content.Intent.ACTION_SEND);
                targetedShare.setType(为image / jpeg); //这里把你的MIME类型
                如果(info.activityInfo.packageName.toLowerCase()。包括(nameApp)|| info.activityInfo.name.toLowerCase()。包括(nameApp)){
                    targetedShare.putExtra(Intent.EXTRA_SUBJECT,文本);
                    targetedShare.putExtra(Intent.EXTRA_TEXT,文本);
                    targetedShare.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(新文件(的ImagePath)));
                    targetedShare.setPackage(info.activityInfo.packageName);
                    targetedShareIntents.add(targetedShare);
                }
            }
            意图chooserIntent = Intent.createChooser(targetedShareIntents.remove(0),选择应用程序共享);
            chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,targetedShareIntents.toArray(新Parcelable [] {}));
            startActivity(chooserIntent);
    }
}
赶上(例外五){
}
}
 

使用此为Facebook: -

 尝试{
文件文件路径=/mnt/sdcard/vmphoto.jpg; //这是你的变化是ACC镜像文件的路径。您的要求。
每股(脸谱,filePath.toString(),你好); <<<<< Hello是文本。发送累计。你REQ。

}
赶上(例外五){
       //可能你的应用程序,如Gmail,Facebook的等未安装异常发生或无法正常工作。
}
 

有关微博

 尝试{
文件文件路径=/mnt/sdcard/vmphoto.jpg; //这是你的变化是ACC镜像文件的路径。您的要求。
每股(推特,filePath.toString(),你好); <<<<< Hello是一个文本发送ACC。你REQ。

}
赶上(例外五){
       //可能你的应用程序,如Gmail,Facebook的等未安装异常发生或无法正常工作。
}
 

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 but the text that needs to be shared is in an EditText in the app so my question is how can i insert my text from the EditText to te text that will be shared (sorry for the bad english if you need further explanation or you didn't understood anything then i will try too explain it better). This is my code for both sharing methods:

Facebook:

Intent shareIntent = new Intent(
                    android.content.Intent.ACTION_SEND);
            shareIntent.setType("text/plain");
            shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                    (String) v.getTag(R.string.app_name));
            shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
                    (String) v.getTag(R.drawable.ic_launcher));
            PackageManager pm = v.getContext().getPackageManager();
            List<ResolveInfo> activityList = pm.queryIntentActivities(
                    shareIntent, 0);
            for (final ResolveInfo app : activityList) {
                if ((app.activityInfo.name).contains("facebook")) {
                    final ActivityInfo activity = app.activityInfo;
                    final ComponentName name = new ComponentName(
                            activity.applicationInfo.packageName,
                            activity.name);
                    shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                    shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                            | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                    shareIntent.setComponent(name);
                    v.getContext().startActivity(shareIntent);
                    break;
                }
            }

Twitter:

Intent shareIntent = new Intent(
                    android.content.Intent.ACTION_SEND);
            shareIntent.setType("text/plain");
            shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                    (String) v.getTag(R.string.app_name));
            shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
                    (String) v.getTag(R.drawable.ic_launcher));
            PackageManager pm = v.getContext().getPackageManager();
            List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
            for (final ResolveInfo app : activityList) {
                if ("com.twitter.android.PostActivity"
                        .equals(app.activityInfo.name)) {
                    final ActivityInfo activity = app.activityInfo;
                    final ComponentName name = new ComponentName(
                            activity.applicationInfo.packageName,
                            activity.name);
                    shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
                    shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                            | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                    shareIntent.setComponent(name);
                    v.getContext().startActivity(shareIntent);
                    break;
                }
            }

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.

private void share(String nameApp, String imagePath,String text) {
try
{
    List<Intent> targetedShareIntents = new ArrayList<Intent>();
    Intent share = new Intent(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 = new Intent(android.content.Intent.ACTION_SEND);
                targetedShare.setType("image/jpeg"); // put here your mime type
                if (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(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){
}
}

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.
}