Sapna Sharma Sapna Sharma - 2 years ago 170
Android Question

How to add a custom option(Dowload Image) with other share options in android

Is it possible to add a custom option(For eg. Download Image) with other share option(shown in attached image) in intent chooser?

enter image description here

Answer Source

Unfortunately, I did not get useful answers. I did a workaround to solve this.

Here is the code to add a custom 'Save to Gallery" option(shown in attached image)

Intent downloadIntent = new Intent(aContext, DownloadActivity.class);
downloadIntent.putExtra("url", url);
intentList.add(new LabeledIntent(downloadIntent, "com.don.offers", "Save To Gallery", R.drawable.ic_save_to_gallery));

// convert intentList to array
LabeledIntent[] extraIntents = intentList.toArray(new LabeledIntent[intentList.size()]);
openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);

Here DownloadActivity is transparent activity that has the code to download image.

public class DownloadActivity extends AppCompatActivity {

    String imageUrl;

    protected void onCreate(Bundle savedInstanceState) {

        imageUrl = getIntent().getStringExtra("url");

    public void saveToGallery() {
        try {
            String DIR_NAME = "Don Downloaded Images";
            String filename = "DON_IMG_" + System.currentTimeMillis();
            //String downloadUrlOfImage = "";
            String downloadUrlOfImage = imageUrl;
            File direct =
                    new File(Environment
                            .getAbsolutePath() + "/" + DIR_NAME + "/");

            if (!direct.exists()) {
                Log.e("saveToGallery", "dir created for first time");

            DownloadManager dm = (DownloadManager) DownloadActivity.this.getSystemService(Context.DOWNLOAD_SERVICE);
            Uri downloadUri = Uri.parse(downloadUrlOfImage);
            DownloadManager.Request request = new DownloadManager.Request(downloadUri);
            request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE)
                            File.separator + DIR_NAME + File.separator + filename);


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                final Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                final Uri contentUri = Uri.fromFile(direct);
            } else {
                final Intent intent1 = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()));
        } catch (Exception e) {

enter image description here

If anyone has better approach please share.Thanks

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download