我有以下代码来打开共享意图,但它被禁用并且无法单击。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.share, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
break;
case R.id.action_share:
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
File folder = new File(Environment.getExternalStorageDirectory() + "/TESTFOLDER");
if (!folder.exists()) {
folder.mkdir();
}
try {
if (file.exists()) {
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivityForResult(Intent.createChooser(share, "Share Chart"), 1);
}
else {
//displayToast("Please save your image before sharing.");
}
}
catch (Exception e) {
e.printStackTrace();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
分享xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/action_share"
android:showAsAction="always"
android:title="Share"
android:actionProviderClass="android.widget.ShareActionProvider" />
</menu>
共享图标在我的操作栏中被禁用: