add image to twitter share intent android

后端 未结 3 1958
执念已碎
执念已碎 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<ResolveInfo> 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();
        }
    }
    
    0 讨论(0)
  • 2021-02-08 11:16

    Here is solution:

    private fun shareOnTwitter() {
        val file = File(context!!.filesDir, FILENAME_SHARE_ON_TWITTER)
        val uriForFile = FileProvider.getUriForFile(context!!, com.yourpackage.activity.YourActivity, file)
    
        val intent = Intent(Intent.ACTION_SEND).apply {
            type = "image/jpeg"
            putExtra(Intent.EXTRA_STREAM, uriForFile)
        }
        startActivity(intent)
    }
    
    0 讨论(0)
  • 2021-02-08 11:17

    This is what you need

    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file);
    
    0 讨论(0)
提交回复
热议问题