Handle ShareActionProvider onClick event
Asked Answered
P

4

6

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!

Phonetic answered 29/10, 2013 at 11:41 Comment(0)
E
8

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;
    }
} 
Exist answered 3/12, 2013 at 10:8 Comment(0)
P
1

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.

Proclaim answered 29/10, 2013 at 12:21 Comment(0)
T
0

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

Titled answered 29/10, 2013 at 11:57 Comment(1)
it didn't works... menuShare = menu.findItem(R.id.menu_item_share); menuShare.setOnMenuItemClickListener(new OnMenuItemClickListener() { @Override public boolean onMenuItemClick(MenuItem item) { /*should stop here*/ return false; } });Phonetic
S
0

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());
Symer answered 19/11, 2013 at 13:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.