共享图像意图与自定义保存到图库选项 [英] Share Image Intent with custom Save To Gallery option

查看:202
本文介绍了共享图像意图与自定义保存到图库选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android的默认分享意图将图片分享给其他应用。但我想添加一个选项,如保存到图库,它会将图像直接保存到媒体或图像或我的应用程序文件夹中的图库。

I am using Android's default share intent to share an image to other apps. But I would like to add an option like Save to Gallery which saves that Image directly to Gallery in media or images or my app's folder.

目前我使用简单代码分享图片:

Currently I am using the simple code to share image:

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, "Share to"));

当我从个人资料图片中尝试WhatsApp的分享按钮时,有一个选项保存到图库选项。现在,我开始知道,它不是预先构建在android中。这是WhatsApp添加的手动操作,它以某种方式将图像保存到特定路径。长按保存到图库按钮显示该操作属于WhatsApp的应用程序。这意味着WhatsApp已经为它编写了一些逻辑。它与图像/ * mime类型具有相同的默认 ACTION_SEND 意图,并带有其他自定义操作保存到图库

When I tried WhatsApp's share button from profile picture, there is an option of "Save To Gallery" Option. Now, I came to know that, it is not pre-build in android. That is the manual action added by WhatsApp which somehow saves an image to specific path. On Long pressing that "Save to gallery" button shows that, that action is of WhatsApp's app. So that means WhatsApp has written some logic for it. It is the same default ACTION_SEND intent with image/* mime type with an additional custom action "Save to Gallery".

这完全没问题。我也希望为我的应用添加这种逻辑。我可以注册< intent-filter> ,它可以处理此操作,但它会全局接受来自任何应用的任何图像以保存到图库。

That's totally fine. I want to add that kind of logic for my app, too. I can register <intent-filter> which can handle this action but it will globally accept any image from any app to save to gallery.

如何为我的共享Intent或< intent-filter> 活动添加手动操作特定于我的应用程序(仅限我的应用程序可见)并用于将图像保存到图库?

How can I add a manual action for my sharing Intent or <intent-filter> activity which can be specific to my app only (visible by my app only) and serves the purpose of saving an Image to Gallery?

推荐答案

以下snippet执行以下操作:
首先我们查询系统中的IntentActivities,它可以处理我们的Share-Intent。之后我们构建了一个LabeledIntents列表。最后,我们将自定义共享操作添加到此列表中,我们将使用默认选择器对话框向用户显示列表。

The following snippet does the following: At first we are querying the system for IntentActivities, which can handle our Share-Intent. After that we build a List of LabeledIntents. Finally we add our custom share action to this list and we will present the list to the user, with the default chooser dialog.

public Intent getNativeShareIntent(final Context context) {
  final String defaultChannel = shareObject.getDefaultChannel();

  final PackageManager pm = context.getPackageManager();
  final Intent sendIntent = new Intent(Intent.ACTION_SEND);
  sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
  sendIntent.setType("image/jpeg");      
  List<ResolveInfo> resInfo = pm.queryIntentActivities(sendIntent, 0);
  List<LabeledIntent> intentList = new ArrayList<>();

  for (int i = 0; i < resInfo.size(); i++) {
        ResolveInfo ri = resInfo.get(i);
        String packageName = ri.activityInfo.packageName;         
        final Intent intent = new Intent();
        intent.setComponent(new ComponentName(packageName, ri.activityInfo.name));
        intent.setPackage(packageName);
        intent.setAction(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
        intent.setType("image/jpeg");      
        intentList.add(new LabeledIntent(intent, packageName, ri.loadLabel(pm),
              ri.getIconResource()));
  }

  // TODO Implement the method getSaveToGalleryIntent, 
  // Could be a simple intent linking to activity.
  intentList.add(getSaveToGalleryIntent(context));

  Intent openInChooser = Intent.createChooser(intentList.remove(0),
        "Share");
  LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);
  openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);
  return openInChooser;
}

 private Intent getSaveToGalleryIntent(final Context context, final Uri imgUrl) {
     final Intent intent = new Intent(context, SaveToGalleryActivity.class);
     intent.putExtra(SaveToGalleryActivity.EXTRA_KEY_CONTENT, imgUrl);
  return new LabeledIntent(intent, BuildConfig.APPLICATION_ID,
        "Save to gallery",
        R.drawable.ic_save_to_gallery);
 }

这篇关于共享图像意图与自定义保存到图库选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆