add image to twitter share intent android

后端 未结 3 2006
执念已碎
执念已碎 2021-02-08 10:42

I\'m trying to add an image to my twitter share intent. I save an image locally in one class and then in another I get the image and try to attach to my intent.

Here is

3条回答
  •  没有蜡笔的小新
    2021-02-08 11:01

    This might be helpful for somebody:

    private void sendShareTwit() {
        try {
            Intent tweetIntent = new Intent(Intent.ACTION_SEND);
    
            String filename = "twitter_image.jpg";
            File imageFile = new File(Environment.getExternalStorageDirectory(), filename);
    
            tweetIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.twitter_share_text));
            tweetIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
            tweetIntent.setType("image/jpeg");
            PackageManager pm = getActivity().getPackageManager();
            List lract = pm.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);
            boolean resolved = false;
            for (ResolveInfo ri : lract) {
                if (ri.activityInfo.name.contains("twitter")) {
                    tweetIntent.setClassName(ri.activityInfo.packageName,
                            ri.activityInfo.name);
                    resolved = true;
                    break;
                }
            }
    
            startActivity(resolved ?
                    tweetIntent :
                    Intent.createChooser(tweetIntent, "Choose one"));
        } catch (final ActivityNotFoundException e) {
            Toast.makeText(getActivity(), "You don't seem to have twitter installed on this device", Toast.LENGTH_SHORT).show();
        }
    }
    

提交回复
热议问题