4

I want to add sharing button to my app, and I have done the following:

final Intent shareIntent = new Intent(Intent.ACTION_SEND);              
            /* Fill it with Data */
            shareIntent.setType("plain/text");
            shareIntent.putExtra(Intent.EXTRA_TEXT, "www.somesite.com");    


            /* Send it off to the Activity-Chooser */
            startActivity(Intent.createChooser(shareIntent, "Share..."));

It shows a dialog and I dont see in this dialog facebook and twitter. I do have both these applicaitons installed in my phone. So, first question is why it doesnt show them? And second if later I will make them somehow appear in the phone, how to make that dialog show only facebook and twitter, and if user does not have them, ask user just to install it by giving link to official app.

Daler
  • 1,205
  • 3
  • 18
  • 39

2 Answers2

5

You can check them by using below code,

How to customize share intent in Android?

Android Intent for Twitter application

I've seen a lot of questions about modifying the app chooser, and they all seem to state that no, you cannot change the built-in app chooser, but you can create a custom app chooser using queryIntentActivities() in the PackageManager class.

try{
    ApplicationInfo info = getPackageManager().getApplicationInfo("com.facebook.katana", 0 );
    return true;
} catch( PackageManager.NameNotFoundException e ){
    return false;
}


try{
    ApplicationInfo info = getPackageManager().getApplicationInfo("com.twitter.android", 0 );
    return true;
} catch( PackageManager.NameNotFoundException e ){
    return false;
}
Community
  • 1
  • 1
Talha
  • 12,673
  • 5
  • 49
  • 68
  • So, in this case i must not use intents. I have to come out with my own dialog, and then somehow send them to app. Still confused, but +1 for some idea ) – Daler Dec 25 '12 at 15:09
1

After Successfully login completion.

public class ShareOnTwitterTrophy extends AsyncTask<String, Integer, Long> {
    private Activity mActivity;
    private Bitmap bitmap;
    public ShareOnTwitterTrophy(Activity mActivity,Bitmap bitmap) {
        this.mActivity=mActivity;
        this.bitmap=bitmap;
    }

    protected void onPreExecute() {
    }

    @Override
    protected Long doInBackground(String... arg0) {

        long result = 0;
        // TwitterSession twitterSession = new TwitterSession(activity);
        // AccessToken accessToken = twitterSession.getAccessToken();
        AccessToken accessToken = new UserSharedPreference(mActivity).getTwitterAccessToken();
        if (accessToken != null) {
            Configuration conf = new ConfigurationBuilder()
                    .setOAuthConsumerKey("your key")
                    .setOAuthConsumerSecret(
                            "your secret")
                    .setOAuthAccessToken(accessToken.getToken())
                    .setOAuthAccessTokenSecret(accessToken.getTokenSecret())
                    .build();

            ImageUploadFactory factory = new ImageUploadFactory(conf);
            ImageUpload upload = factory.getInstance();
            Log.d("", "Start sending image...");
            try {
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();

                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

                // you can create a new file name "test.jpg" in sdcard
                // folder.
                String imagePath = Environment
                        .getExternalStorageDirectory()
                        + File.separator
                        + "test.jpg";
                File f = new File(imagePath);
                f.createNewFile();
                // write the bytes in file
                FileOutputStream fo = new FileOutputStream(f);
                fo.write(bytes.toByteArray());

                // remember close de FileOutput
                fo.close();
                upload.upload(f, "");
                Log.e("Image Uploaded", "yayeeeee");
                result = 1;
            } catch (Exception e) {
                Log.e("image upload failed", "awwwww :(");
                e.printStackTrace();
            }

            return result;
        }
        return result;
    }

    @Override
    protected void onPostExecute(Long result) {
        if (result == 1)
            Toast.makeText(
                    mActivity,
                    mActivity
                            .getString(R.string.twitter_shared_successfully),
                    Toast.LENGTH_LONG).show();
    }
Micha
  • 5,117
  • 8
  • 34
  • 47