Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android share intent Twitter: How to share only by Tweet or Direct Message?

I need to share an image via only Tweet.

Here is my code

Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("image/*");
        if (mInsertableToGallery) {
            mInsertableToGallery = false;
            mInsertedImagePath = MediaStore.Images.Media.insertImage(getContentResolver(), mShareImage,
                    getResources().getString(R.string.app_name) + System.currentTimeMillis(), getResources().getString(R.string.app_name));
        }
        // share only 5 specific apps:
        List<Intent> targetedShareIntents = new ArrayList<>();
        List<ResolveInfo> resInfos = getPackageManager().queryIntentActivities(shareIntent, 0);
        if (!resInfos.isEmpty()) {
            for (ResolveInfo resolveInfo : resInfos) {
                String packageName = resolveInfo.activityInfo.packageName;
                Intent targetedShareIntent = new Intent(Intent.ACTION_SEND);
                targetedShareIntent.setType("image/*");
                targetedShareIntent.setPackage(packageName);
                targetedShareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(mInsertedImagePath));
                if (TextUtils.equals(packageName, "com.facebook.katana") //fb
                ||TextUtils.equals(packageName, "com.instagram.android") //instagram
                ||TextUtils.equals(packageName, "jp.naver.line.android") //line
                ||TextUtils.equals(packageName, "com.google.android.apps.plus") // plus
                ||TextUtils.equals(packageName, "com.twitter.android")) // twitter
            {
                targetedShareIntents.add(targetedShareIntent);
            }

            }
        }
        Intent chooser = Intent.createChooser(targetedShareIntents.remove(0), "");
        chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
        startActivity(chooser);

and I get the result: share only via 5 apps

When click "Android System", Twitter share popup: share Twitter

What I want:

  1. How to share via Twitter only by Tweet or Direct Message ?

  2. How to change title & icon of share item. Ex Twitter share item from "Android System" to "Tweet"?

like image 685
Cuong Nguyen Avatar asked Sep 26 '22 10:09

Cuong Nguyen


1 Answers

I couldn't manage to get both Tweet and Direct Message to the front but the following code will get you either one there. Actually you can get both to the front but both appears with same name "Tweet" so its kind of unusable. I have also included facebook, insta and tumblr. To get Direct Message in place of tweet replace com.twitter.android.composer.ComposerActivity with com.twitter.android.DMActivity. To get both add another ' if ' for DMActivity.

Can't say surely but I don't think you can change the Icon of the package for your app.

List<Intent> targetedShareIntents = new ArrayList<Intent>();
        Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
        shareIntent.setType("image/jpeg");//("text/plain");
        Uri uri = Uri.fromFile(outFile);
        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
        List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(shareIntent, 0);
        for (ResolveInfo resolveInfo : resInfo) {
            String packageName = resolveInfo.activityInfo.packageName;
            Intent targetedShareIntent = new Intent(android.content.Intent.ACTION_SEND);
            targetedShareIntent.setType("image/jpeg");
            targetedShareIntent.putExtra(Intent.EXTRA_STREAM, uri);
            if(TextUtils.equals(packageName,"com.facebook.katana")|TextUtils.equals(packageName,"com.instagram.android")
                    |TextUtils.equals(packageName,"com.tumblr")) {
                targetedShareIntent.setPackage(packageName);
                targetedShareIntents.add(targetedShareIntent);
            }
            if(TextUtils.equals(resolveInfo.activityInfo.name,"com.twitter.android.composer.ComposerActivity")) {
                targetedShareIntent.setPackage(packageName);
                targetedShareIntent.setClassName(
                        resolveInfo.activityInfo.packageName,
                        resolveInfo.activityInfo.name);
                targetedShareIntents.add(targetedShareIntent);
            }
        }
        Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
        startActivity(chooserIntent);
like image 91
Deepak Soni Avatar answered Sep 30 '22 06:09

Deepak Soni