0

ユーザーが写真を撮ったときに提供されるカメラを使用するアプリを作成しましたShareactionproviderApiレベル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();
    }
}

何が間違っているのですか?

4

1 に答える 1