//功能:点击中间机器人图标打开图库 然后选中要分享的图片
//在点击选择菜单分享图标 分享到需要分享的地方
效果示例图:
1、选择菜单 share_menu.xml 布局
代码
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/actionprovider"
android:actionProviderClass="android.widget.ShareActionProvider"
android:showAsAction="always"
android:title=""/>
</menu>
===================
2、主布局activity_main.xml
代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<ImageView
android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:layout_centerInParent="true"
android:onClick="onclickImage"/>
</RelativeLayout>
==============
3、MainActivity类
代码
public class MainActivity extends Activity {
private ShareActionProvider provider;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.imageView = (ImageView) this.findViewById(R.id.imageview);
}
//图片点击事件监听
public void onclickImage(View view){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);//打开图库的动作设置
intent.setType("image/*");
int requestCode = 1;
startActivityForResult(intent, requestCode );
}
//打开图库后选择图片后的回调方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == Activity.RESULT_OK){
Uri uri = data.getData();
try {
ContentResolver resolver = getContentResolver();
InputStream is = resolver.openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(is);
imageView.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
//设置分享的数据和类型
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, uri);
//通过setShareIntent方法设置要分享的数据
provider.setShareIntent(intent);
}
}
//创建一个选择菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.share_menu, menu);
/*//文本分享
shareTxt(menu);*/
//分享图片
shareImg(menu);
return super.onCreateOptionsMenu(menu);
}
private void shareImg(Menu menu) {
MenuItem item_provider = menu.findItem(R.id.actionprovider);
provider = (ShareActionProvider) item_provider.getActionProvider();
}
/*//分享文本的方法
public void shareTxt(Menu menu) {
MenuItem item_provider = menu.findItem(R.id.actionprovider);
provider = (ShareActionProvider) item_provider.getActionProvider();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "这是文本信息");
//用ShareActionProvider发送意图
provider.setShareIntent(intent);
}*/
}