我开发了一个操作栏共享按钮,但是当我运行该应用程序时,该按钮确实可以工作,但是当我尝试与其他应用程序共享图像时,它会使应用程序崩溃。例如,如果我与 Twitter 或消息共享图像,应用程序就会崩溃并说“Twitter 或消息已崩溃。
请提供我要分享的图像。
private final static int[] resourceIDs = new int[]{R.drawable.attraction1, R.drawable.attraction2,
R.drawable.attraction3, R.drawable.attraction4, R.drawable.attraction5,
R.drawable.attraction6, R.drawable.attraction7, R.drawable.attraction8,
R.drawable.attraction9, R.drawable.attraction10, R.drawable.attraction11,
R.drawable.attraction12, R.drawable.attraction13, R.drawable.attraction14,
R.drawable.attraction15, R.drawable.attraction16, R.drawable.attraction17,
R.drawable.attraction18, R.drawable.attraction19, R.drawable.attraction20};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_share, menu);
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_item_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(item);
// Return true to display menu
return true;
}
//Actionbar and toolbar icons onclick methods.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.menu_item_share) {
String text = "Look at my awesome picture";
Uri pictureUri = Uri.parse("android.resource://\" + getPackageName()\n" +
" + \"/drawable\"");
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share images..."));
}
int id1 = item.getItemId();
if (id1 == android.R.id.home) {
Intent intent= new Intent(this,MenuActivity.class);
startActivity(intent);
}
return super.onOptionsItemSelected(item);
}
// Call to update the share intent
private void setShareIntent(Intent shareIntent) {
if (mShareActionProvider != null) {
mShareActionProvider.setShareIntent(shareIntent);
}
}
}
Share_Menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:support="http://schemas.android.com/tools"
android:label="@string/app_name"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_item_share"
app:showAsAction="ifRoom"
android:title="Share"
android:actionProviderClass=
"android.widget.ShareActionProvider" />
</menu>
请帮助和建议,我不明白为什么共享应用程序崩溃而我自己的应用程序没有。