如何获取ShareActionProvider菜单项的onClick事件?
我的应用程序在库中显示一些图像(带有imageView的片段),我需要获取onClick事件以从缓存加载当前图像,生成临时文件并共享它。
我的分享按钮:
<item android:id="@+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="@string/menu_share"
android:actionProviderClass="android.widget.ShareActionProvider" />
我试过了:
@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);
}
和
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;
}
});
还有其他建议吗? 谢谢!
答案 0 :(得分:8)
每次用户点击ShareActionProvider时,都要将要共享的图像保存到SD卡中,完整示例:
@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;
}
}
答案 1 :(得分:1)
ShareActionProvider
不支持您建议的流程。您将需要分叉或提出其他一些共享解决方案。
或者,更好的是,实施可以提供图片的ContentProvider
,这样您就可以首先使用Intent
ShareActionProvider
,而不需要更改它根据用户选择的共享目标。通过使用您自己的ContentProvider
,您可以完全避免使用临时文件。根据图像的存储位置,FileProvider
may be sufficient for your needs。
答案 2 :(得分:0)
我没有检查过,但我会尝试使用OnMenuItemClickListener事件
答案 3 :(得分:0)
你可以使用ShareActionProvider 的OnShareTargetSelectedListenener然后将图片添加到你的意图(你不能在这里修改你的itent):
mShareActionProvider = (ShareActionProvider) menuItemShare.getActionProvider();
mShareActionProvider.setOnShareTargetSelectedListener(new OnShareTargetSelectedListener() {
@Override
public boolean onShareTargetSelected(ShareActionProvider actionProvider, Intent intent) {
return false;
}
});
所以另一种解决方案是,当您更改当前图像时更新您的意图:
mShareActionProvider.setShareIntent(createIntentForCurrentImage());