使用意图Android在Facebook上分享照片?

时间:2014-05-17 10:32:55

标签: android facebook image android-intent twitter

我想使用意图发送照片 我已经尝试了这个代码,但我的问题是,如果我使用此代码进行共享,而不是打开所有与共享相关的applciation我想只打开FACEBOOK和TWITTER APP进行共享

Intent sharefacebook = new Intent(Intent.ACTION_SEND); 
            sharefacebook.setType("image/*");
            sharefacebook.putExtra(Intent.EXTRA_TEXT, "From Android");              
            sharefacebook.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+filepath.toString()));
            startActivity(Intent.createChooser(sharefacebook, "Sharing"));

它打开电子邮件,skype,gmail everthing我想只打开facebook和twitter这是可能的

1 个答案:

答案 0 :(得分:1)

您可以查询客户端应用程序(活动)并仅在它是这样的fb / twitter客户端时启动活动:

   final PackageManager pm = getPackageManager();
                    final List<ResolveInfo> matches = pm.queryIntentActivities(shareIntent, 0);
            for (final ResolveInfo info : matches){
                if (info.activityInfo.name.toLowerCase().contains("facebook") || info.activityInfo.name.toLowerCase().contains("twitter")){ 
        Intent sharefacebook = new Intent(Intent.ACTION_SEND); 
                    sharefacebook.setType("image/*");
                    sharefacebook.putExtra(Intent.EXTRA_TEXT, "From Android");              
                    sharefacebook.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+filepath.toString()));                              
                   startActivity(Intent.createChooser(sharefacebook, "Sharing"));   
            }else{
                                Toast.makeText(getApplicationContext(), "FB or twitter client not installed", Toast.LENGTH_SHORT).show();

            }
    }