如何在安卓系统中自定义分享菜单?

4
我需要分享图片并在Android分享菜单中添加“保存图片”选项,我在9gag应用程序中看到过类似的功能,他们在分享菜单中有“保存”选项,并且分享菜单似乎是一个底部表。但这该如何实现呢? enter image description here 我已经完成了以下工作: 我在清单文件中添加了一个具有意图过滤器的空活动,该活动启动服务并下载图片。
<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>

现在我在共享菜单中有这个图标,它可以工作。但是这个图标也出现在其他应用程序的共享菜单中,我需要仅在我的应用程序中显示它,我该如何使它私有或其他方式?

图标也会出现在其他应用程序中吗?难道你想说的是其他活动吗? - Brandon Zamudio
是的,它出现在其他应用程序中,甚至安卓相册中也有我的应用程序的“保存”选项在共享图像菜单中,我想从其他应用程序的共享菜单中排除我的“保存”活动。 - Wackaloon
1个回答

2

好的,我找到了一个解决方案。首先,我们需要一个处理图像保存意图的活动,这个活动可以启动服务或其他东西。我是这样做的:

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();
    }
}

SaveImageService中有一个静态方法,用于处理将图像保存到SD卡中。其次,我们需要在清单文件中添加一些文本:

    <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>

这里的意图过滤器具有自定义操作(这很重要),此自定义操作仅是某些字符串,而不是应用程序包或其它什么(我仅仅使用了应用程序包名称因为我喜欢它)。 接下来,我们需要将此活动添加到共享菜单列表:

这将从ImageView获取位图Uri以在其他应用程序中共享。

// 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;
    }

这将收集所有可以共享图像以及我们的保存图像意图的应用程序

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);
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接