Handle the ShareActionProvider onClick event

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

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

My sharing button:

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

I 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!

+6
source share
4 answers

Save the image you want to share on the SD card every time the user clicks on 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; } } 
+8
source

Your suggested thread is not supported by ShareActionProvider . You will need to unlock it or come up with some other solution for sharing.

Or, even better, implement a ContentProvider that can serve the image, so you can use the correct Intent with the ShareActionProvider in the first place and should not change it based on the user's choice of sharing purpose. Using your own ContentProvider , you can probably avoid the temp file altogether. Depending on where your images are stored, FileProvider may be enough for your needs .

+1
source

I did not test it, but instead I would try the OnMenuItemClickListener event

0
source

You can use OnShareTargetSelectedListenener for ShareActionProvider , and then add the image to your intention (here you are not allowed to change your itent):

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

So another solution would be that you update your intention when changing the current image:

 mShareActionProvider.setShareIntent(createIntentForCurrentImage()); 
0
source

Source: https://habr.com/ru/post/956939/


All Articles