Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share Image Intent with custom Save To Gallery option

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

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

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.

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?

like image 451
kirtan403 Avatar asked Mar 13 '23 08:03

kirtan403


1 Answers

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 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);
 }
like image 149
Christopher Avatar answered Mar 24 '23 00:03

Christopher