[android] ShareActionProvider

사진을 공유할 때, 그 동안은 intent를 만들고, startActivity를 호출하는 것만 썼었는데, 새로 무언가가 나왔다.

 

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

 

private ShareActionProvider _shareActionProvider;
.
.
.
_file = new File("/folder/image.jpg");
.
.
.
.
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
  getMenuInflater().inflate(R.menu.show_photo, menu);
  MenuItem item = menu.findItem(R.id.menu_item_share);
  _shareActionProvider = (ShareActionProvider) item.getActionProvider();
  _shareActionProvider.setShareHistoryFileName(ShareActionProvider.DEFAULT_SHARE_HISTORY_FILE_NAME);
  _shareActionProvider.setShareIntent(createShareIntent());
  return true;
}
 
private Intent createShareIntent()
{
  Intent intent = new Intent(Intent.ACTION_SEND);
  intent.setType("image/*");
  intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(_file)); // don't use Uri.parse()
  return intent;
}

 

이렇게 하면 아래처럼 나온다.

screenshot_2014-10-30_PM 3.53.25

 

screenshot_2014-10-30_PM 3.53.50