Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to startActivity with a ShareCompat.IntentBuilder

I'm attempting to add a shared intent to post to Google Plus and can't seem to resolve the issue of passing the new ShareCompat.IntentBuilder (Android support library class) to the startActivity method. I'm started out using this example. My app is compiled using Android 2.2 platform. Is it possible that there is another supporting way to start the Activity to launch the shared intent.

IntentBuilder shareIntent = ShareCompat.IntentBuilder.from(MyActivity.this);                
shareIntent.setText(message);
shareIntent.setSubject(subject);

if (mFullFileName != null) {
    File imageFile = new File(mFullFileName);
    if (imageFile.exists()) {
        shareIntent.setStream(Uri.fromFile(imageFile));
        shareIntent.setType("image/jpeg");
    }
} else {
    shareIntent.setType("*.*");
}   
shareIntent.getIntent();
// doesn't compile only accepts Intent and not the Intentbuilder 
startActivity(shareIntent); 
like image 561
user167698 Avatar asked Jun 08 '12 04:06

user167698


People also ask

What is ShareCompat IntentBuilder class do?

IntentBuilder is a helper for constructing ACTION_SEND and ACTION_SEND_MULTIPLE sharing intents and starting activities to share content. The ComponentName and package name of the calling activity will be included.

What is ShareCompat in Android?

ShareCompat provides functionality to extend the ACTION_SEND / ACTION_SEND_MULTIPLE protocol and support retrieving more info about the activity that invoked a social sharing action. IntentBuilder provides helper functions for constructing a sharing intent that always includes data about the calling activity and app.


2 Answers

ShareCompat.IntentBuilder.from(ActivityName.this) is deprecated, use the constructor of IntentBuilder like this:

Kotlin:

    ShareCompat
               .IntentBuilder(this@YourActivity)
               .setType("text/plain")
               .setChooserTitle("Share text with: ")
               .setText("Desired text to share")
               .startChooser()

Java:

new ShareCompat
                .IntentBuilder(YourActivity.this)
                .setType("text/plain")
                .setChooserTitle("Share text with: ")
                .setText("Desired text to share")
                .startChooser();
like image 51
Ahmed Maad Avatar answered Nov 15 '22 18:11

Ahmed Maad


Thats an example from my code, but if you want some reference material tap on the hyperlink for an article.

public void shareText(String text) {
        String mimeType = "text/plain";
        String title = "Example title";

        Intent shareIntent =   ShareCompat.IntentBuilder.from(this)
                                                    .setType(mimeType)
                                                    .setText(text)
                                                    .getIntent();
        if (shareIntent.resolveActivity(getPackageManager()) != null){
            startActivity(shareIntent);
        }
    }

Blog post on ShareCompat.IntentBuilder and sharing intents

like image 35
klenki Avatar answered Nov 15 '22 18:11

klenki