Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 570
Eric Waideman Avatar asked Oct 29 '13 11:10

Eric Waideman


2 Answers

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;
    }
} 
like image 94
Tony Ceralva Avatar answered Nov 06 '22 22:11

Tony Ceralva


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.

like image 44
CommonsWare Avatar answered Nov 06 '22 22:11

CommonsWare