Wackaloon
Wackaloon

Reputation: 2365

How to customize share menu in android?

I need to share image and add "Save image" item in android share menu, I saw something like this in 9gag app, they have "Save" item in share menu, and share menu seems to be a bottom sheet. But how it can be achieved? enter image description here

What I've done: I added empty activity with intent filter in manifest which launches service and this service downloads image

<activity
            android:name=".model.services.ShareActivity"
            android:icon="@drawable/download_icon"
            android:label="Save">
            <intent-filter
                android:label="Save"
                android:icon="@drawable/download_icon">
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="image/*"/>
            </intent-filter>
        </activity>

and now I have this icon in share menu and it works, but this icon also appear in share menu of other apps, I need to show it only in my app, how I can make it private or something?

Upvotes: 4

Views: 3682

Answers (1)

Wackaloon
Wackaloon

Reputation: 2365

Ok, I found a solution. First - we need activity which will handle image saving intent, this activity can launch service or something. I make it like this:

public class ShareActivity extends Activity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle extras = getIntent().getExtras();
        String url = extras.getString("url");
        String name = extras.getString("name");
        String description = extras.getString("description");
        SaveImageService.downloadFile(url, name, description);
        finish();
    }
}

Where SaveImageService has static method which handle image saving to SD card Second, we need to add some text in manifest:

    <activity
        android:name=".model.services.ShareActivity"
        android:icon="@drawable/download_icon"
        android:label="Save">
        <intent-filter
            android:label="Save"
            android:icon="@drawable/download_icon">
            <action android:name="com.my_app.random_text.SAVE_IMAGE" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:mimeType="image/*"/>
        </intent-filter>
    </activity>

Here intent filter has custom action (this is important) this custom action is just some string, not app package or something (I'm using package name just because I like it). Next, we need to add this activity to share menu list:

this will get bitmap Uri for sharing in other apps from ImageView

// Returns the URI path to the Bitmap displayed in specified ImageView
    static public Uri getLocalBitmapUri(ImageView imageView) {
        // Extract Bitmap from ImageView drawable
        Drawable drawable = imageView.getDrawable();
        Bitmap bmp = null;
        if (drawable instanceof BitmapDrawable) {
            bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
        } else {
            return null;
        }
        // Store image to default external storage directory
        Uri bmpUri = null;
        try {
            File file = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png");
            file.getParentFile().mkdirs();
            FileOutputStream out = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 80, out);
            out.close();
            bmpUri = Uri.fromFile(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bmpUri;
    }

And this will collect all apps that can share image plus our save image intent

public void shareExcludingApp(Context ctx, PhotoView snapImage) {
        // Get access to the URI for the bitmap
        Uri bmpUri = ShareTool.getLocalBitmapUri(snapImage);
        if (bmpUri == null) return;

        List<Intent> targetedShareIntents = new ArrayList<>();
        //get all apps which can handle such intent
        List<ResolveInfo> resInfo = ctx.getPackageManager().queryIntentActivities(createShareIntent(bmpUri), 0);
        if (!resInfo.isEmpty()) {
            for (ResolveInfo info : resInfo) {
                Intent targetedShare = createShareIntent(bmpUri);
                //add all apps excluding android system and ourselves
                if (!info.activityInfo.packageName.equals(getContext().getPackageName())
                        && !info.activityInfo.packageName.contains("com.android")) {
                    targetedShare.setPackage(info.activityInfo.packageName);
                    targetedShare.setClassName(
                            info.activityInfo.packageName,
                            info.activityInfo.name);
                    targetedShareIntents.add(targetedShare);
                }
            }
        }
        //our local save feature will appear in share menu, intent action SAVE_IMAGE in manifest
        Intent targetedShare = new Intent("com.my_app.random_text.SAVE_IMAGE"); //this is that string from manifest!
        targetedShare.putExtra(Intent.EXTRA_STREAM, bmpUri);
        targetedShare.setType("image/*");
        targetedShare.setPackage(getContext().getPackageName());
        targetedShare.putExtra("url", iSnapViewPresenter.getSnapUrlForSave());
        targetedShare.putExtra("name", iSnapViewPresenter.getSnapNameForSave());
        targetedShare.putExtra("description", iSnapViewPresenter.getSnapDescriptionForSave());
        targetedShareIntents.add(targetedShare);

        //collect all this intents in one list
        Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0),
                "Share Image");

        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
                targetedShareIntents.toArray(new Parcelable[targetedShareIntents.size()]));

        ctx.startActivity(chooserIntent);
    }

Upvotes: 2

Related Questions