我创建了一个使用相机的应用程序,当用户拍摄照片时,他会被提供Shareactionprovider
(Api 级别 16+)来分享图像......
由于shareactionprovider
必须在用户拍照后出现,所以它必须在onPrepareoptionsmenu()
...
我使用了以下代码,但应用程序崩溃了....如果我不使用 onPrepareoptionsmenu()
该应用程序可以正常工作,但我无法获得 Shareactionprovider
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.camera, menu);
return true;
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
invalidateOptionsMenu();
getMenuInflater().inflate(R.menu.camera, menu);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
// Locate MenuItem with ShareActionProvider
MenuItem item = menu.findItem(R.id.menu_share);
// Fetch and store ShareActionProvider
mShareActionProvider = (ShareActionProvider) item
.getActionProvider();
doShare();
Toast.makeText(this, "prepare", Toast.LENGTH_SHORT).show();
}
return super.onPrepareOptionsMenu(menu);
}
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void doShare() {
try {
if (file.exists() && file.length() > 0) {
// Populate the share intent with data
Intent intent = new Intent(Intent.ACTION_SEND);
mImageUri = Uri.fromFile(file);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, mImageUri);
mShareActionProvider.setShareIntent(intent);
} else
Toast.makeText(
this,
"You cancelled the image capture!!!! Please take image in order to Share",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, "Please take image in order to Share",
Toast.LENGTH_SHORT).show();
}
}
我怎么了?