6

如何获取 ShareActionProvider 菜单项的 onClick 事件?

我的应用程序在图库中显示了一些图像(带有 imageView 的片段),我需要获取 onClick 事件以从缓存中加载当前图像,生成临时文件并共享它。

我的分享按钮:

<item android:id="@+id/menu_item_share"
    android:showAsAction="ifRoom"
    android:title="@string/menu_share"
    android:actionProviderClass="android.widget.ShareActionProvider" />

我试过了:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    case R.id.manage_keywords:
        startActivity(new Intent(this, KeywordActivity.class));
        return true;
    case R.id.menu_item_share:
        /*it should handle share option*/
        return true;
    }
    return super.onOptionsItemSelected(item);
}

    menuShare = menu.findItem(R.id.menu_item_share);
    menuShare.setOnActionExpandListener(new OnActionExpandListener() {

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            // TODO Auto-generated method stub
            return false;
        }
    });

还有什么建议吗??谢谢!

4

4 回答 4

8

每次用户单击 ShareActionProvider 时,将要共享的图像保存到 SDCard,完整示例:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getSupportMenuInflater().inflate(R.menu.menu, menu);

    ShareActionProvider shareActionProvider = (ShareActionProvider) menu.findItem(R.id.shareactionprovider).getActionProvider();
    shareActionProvider.setShareIntent(getShareIntent());
    shareActionProvider.setOnShareTargetSelectedListener(new OnShareTargetSelectedListener() {
        @Override
        public boolean onShareTargetSelected(ShareActionProvider actionProvider, Intent intent) {
            saveImageToSD();
            return false;
        }
    });

    return true;
}




private Intent getShareIntent() {
    Intent shareIntent = new Intent(Intent.ACTION_SEND);

    File sdCard = Environment.getExternalStorageDirectory();

    File sharedFile = new File(sdCard+"/yourPath/yourImage.jpg");
    Uri uri = Uri.fromFile(sharedFile);

    shareIntent.setType("image/*");
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    return shareIntent;
}


private void saveImageToSD() {

    Bitmap bm = BitmapFactory.decodeResource( getResources(), R.drawable.yourimage);

    OutputStream outStream = null;
    try {
        outStream = new FileOutputStream(getTempFile());
        bm.compress(Bitmap.CompressFormat.PNG, 100, outStream);
        outStream.flush();
        outStream.close();
        bm.recycle();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private File getTempFile() {

    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

        File directory = new File(Environment.getExternalStorageDirectory() + "/yourPath/");
        directory.mkdirs();

        File file = new File(directory ,"yourImage.jpg");

        try  {
            file.createNewFile();
        }  catch (IOException e) {}

        return file;
    } else  {
        return null;
    }
} 
于 2013-12-03T10:08:02.227 回答
1

不支持您建议的流程ShareActionProvider。您将需要分叉它或提出其他一些共享解决方案。

或者,更好的是,实现ContentProvider可以提供图像的 a,因此您可以首先使用适当的IntentwithShareActionProvider而无需根据用户选择的共享目标来更改它。通过使用您自己的ContentProvider,您可能可以完全避免使用临时文件。根据您的图像存储位置,FileProvider可能足以满足您的需求

于 2013-10-29T12:21:16.763 回答
0

您可以使用 ShareActionProvider 的 OnShareTargetSelectedListenener ,然后将图片添加到您的意图中(您不能在此处修改您的意图):

mShareActionProvider = (ShareActionProvider) menuItemShare.getActionProvider();
mShareActionProvider.setOnShareTargetSelectedListener(new OnShareTargetSelectedListener() {
        @Override
        public boolean onShareTargetSelected(ShareActionProvider actionProvider, Intent intent) {
            return false;
        }
    });

因此,另一种解决方案是,在更改当前图像时更新您的意图:

mShareActionProvider.setShareIntent(createIntentForCurrentImage());
于 2013-11-19T13:05:48.300 回答
0

我没有检查它,但我会尝试 OnMenuItemClickListener 事件

于 2013-10-29T11:57:43.863 回答