Android:分享选项菜单会在app第一次运行时自动打开

时间:2014-09-12 05:41:30

标签: android menu

我正在制作绘图应用。在我有我设置共享绘图图像。我的问题是,当我第一次启动我的应用程序时,共享菜单会自动打开而不会点击菜单按钮。我想点击菜单按钮时打开。

这是我的menu.xml代码

         <menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
        android:id="@+id/menu_item_share"
        android:showAsAction="ifRoom"
        android:title="Share"
        android:actionProviderClass=
            "android.widget.ShareActionProvider"/>

以下是java文件的代码。

   public boolean onCreateOptionsMenu(Menu menu) {
     /** Inflating the current activity's menu with res/menu/items.xml */
    getMenuInflater().inflate(R.menu.share_menu, menu);

    /** Getting the actionprovider associated with the menu item whose id is share */
    mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.menu_item_share).getActionProvider();

    /** Setting a share intent */
    mShareActionProvider.setShareIntent(getDefaultShareIntent());

    return super.onCreateOptionsMenu(menu);
}

private Intent getDefaultShareIntent(){


    Bitmap bitmap = drawView.getDrawingCache();
    String path = Environment.getExternalStorageDirectory().getAbsolutePath() + UUID.randomUUID().toString()+".png";

    System.out.println("path="+path);
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    Uri screenshotUri = Uri.parse(path);
    //Uri screenshotUri = Uri.fromFile(file);

    sharingIntent.setType("image/png");
    sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
    startActivity(Intent.createChooser(sharingIntent, "Share image using"));
    return sharingIntent;
}

1 个答案:

答案 0 :(得分:2)

startActivity(Intent.createChooser(sharingIntent, "Share image using"));

评论此行