Eric Waideman
Eric Waideman

Reputation: 71

Handle ShareActionProvider onClick event

How I can get onClick event of a ShareActionProvider menu item?

My application shows some images in a gallery (fragments with imageView) and I need get the onClick event to load the current image from cache, generate a temp file and share it.

My share button:

<item android:id="@+id/menu_item_share"
    android:showAsAction="ifRoom"
    android:title="@string/menu_share"
    android:actionProviderClass="android.widget.ShareActionProvider" />

I've tried:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    case R.id.manage_keywords:
        startActivity(new Intent(this, KeywordActivity.class));
        return true;
    case R.id.menu_item_share:
        /*it should handle share option*/
        return true;
    }
    return super.onOptionsItemSelected(item);
}

And

    menuShare = menu.findItem(R.id.menu_item_share);
    menuShare.setOnActionExpandListener(new OnActionExpandListener() {

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            // TODO Auto-generated method stub
            return false;
        }
    });

Any other suggestion?? Thanks!

Upvotes: 6

Views: 3977

Answers (4)

Toni Alvarez
Toni Alvarez

Reputation: 1032

Save the image you want to share to the SDCard every time the user click on the ShareActionProvider, full example:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.menu, menu);

    ShareActionProvider shareActionProvider = (ShareActionProvider) menu.findItem(R.id.shareactionprovider).getActionProvider();
    shareActionProvider.setShareIntent(getShareIntent());
    shareActionProvider.setOnShareTargetSelectedListener(new OnShareTargetSelectedListener() {
        @Override
        public boolean onShareTargetSelected(ShareActionProvider actionProvider, Intent intent) {
            saveImageToSD();
            return false;
        }
    });

    return true;
}




private Intent getShareIntent() {
    Intent shareIntent = new Intent(Intent.ACTION_SEND);

    File sdCard = Environment.getExternalStorageDirectory();

    File sharedFile = new File(sdCard+"/yourPath/yourImage.jpg");
    Uri uri = Uri.fromFile(sharedFile);

    shareIntent.setType("image/*");
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    return shareIntent;
}


private void saveImageToSD() {

    Bitmap bm = BitmapFactory.decodeResource( getResources(), R.drawable.yourimage);

    OutputStream outStream = null;
    try {
        outStream = new FileOutputStream(getTempFile());
        bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
        outStream.flush();
        outStream.close();
        bm.recycle();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private File getTempFile() {

    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

        File directory = new File(Environment.getExternalStorageDirectory() + "/yourPath/");
        directory.mkdirs();

        File file = new File(directory ,"yourImage.jpg");

        try  {
            file.createNewFile();
        }  catch (IOException e) {}

        return file;
    } else  {
        return null;
    }
} 

Upvotes: 8

Wicket
Wicket

Reputation: 403

You could use the OnShareTargetSelectedListenener of the ShareActionProvider and then add the picture there to your intent (you are not allowed to modify your itent here):

mShareActionProvider = (ShareActionProvider) menuItemShare.getActionProvider();
mShareActionProvider.setOnShareTargetSelectedListener(new OnShareTargetSelectedListener() {
        @Override
        public boolean onShareTargetSelected(ShareActionProvider actionProvider, Intent intent) {
            return false;
        }
    });

So the other solution would be, that you update your intent when you change the current image:

mShareActionProvider.setShareIntent(createIntentForCurrentImage());

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006799

Your proposed flow is not supported by ShareActionProvider. You will need to fork it or come up with some other sharing solution.

Or, better yet, implement a ContentProvider that can serve up the image, so you can use a proper Intent with ShareActionProvider in the first place and not need to change it based upon the user's choice of sharing target. By using your own ContentProvider, you can probably avoid the temp file altogether. Depending on where your images are stored, FileProvider may be sufficient for your needs.

Upvotes: 1

petrumo
petrumo

Reputation: 1116

I haven't checked it, but I would try OnMenuItemClickListener event instead

Upvotes: 0

Related Questions