您的位置:首页 > Web前端 > React

在ActionBar显示ShareActionProvider分享文本,点击可以打开进行分享(19)

2016-02-27 00:00 603 查看
摘要: 在ActionBar显示一个分享的图标,点击可以打开进行分享,使用ShareActionProvider系统提供的一个分享提供者

//菜单menu文件夹下创建分享提供者文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
android:id="@+id/share_privder"
android:actionProviderClass="android.widget.ShareActionProvider"
android:orderInCategory="100"
android:showAsAction="always"
android:title=""/>

</menu>
// android:actionProviderClass="android.widget.ShareActionProvider"这个属相一定要设置对
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.share_image);
}

// 点击按钮
public void selectImage(View view) {
// 点击从图库选择图片
Intent intent = new Intent();
// setAction(Intent.ACTION_PICK)另一种设置方式
intent.setAction(Intent.ACTION_GET_CONTENT);// 注意不要用Intentnew出的对象来设置
intent.setType("image/*");
// 打开图库的意图
startActivityForResult(intent, 200);// 带返回值的,选择图片后返回

}

// 图片返回处理方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 200 && resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();// uri通过intent传回来的
// 读取图片显示,通过ContentResolver内容解析器,将uri放进去
ContentResolver resolver = getContentResolver();
try {
InputStream is = resolver.openInputStream(uri);
// 将流转换为位图
Bitmap bitmap = BitmapFactory.decodeStream(is);
imageview.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 设置分享的数据和类型
Intent intentImg = new Intent();
intentImg.setAction(Intent.ACTION_SEND);
intentImg.setType("image/*");
intentImg.putExtra(Intent.EXTRA_STREAM, uri);
//要连通uri,没有uri不懂怎么连通性,即先点中图片按钮再设置数据类型
provider.setShareIntent(intentImg);

}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.share, menu);
// 分享文本
shareText(menu);
// 分享图片
shareImage(menu);
return super.onCreateOptionsMenu(menu);
}

// 分享图片的方法
public void shareImage(Menu menu) {
// 分享图片还是要找到那个分享提供者类
MenuItem shareItem = menu.findItem(R.id.share_privder);
// 使用系统提供的一个分享类
provider = (ShareActionProvider) shareItem.getActionProvider();
}

// 分享文本的方法
public void shareText(Menu menu) {
MenuItem shareItem = menu.findItem(R.id.share_privder);
// 使用系统提供的一个分享类
provider = (ShareActionProvider) shareItem.getActionProvider();
Intent shareintent = new Intent();
// 发送出去的类别
shareintent.setAction(Intent.ACTION_SEND);// 注意不要用Intentnew出的对象来设置
// 指定发送的数据的类型
shareintent.setType("text/plain");
shareintent.putExtra(Intent.EXTRA_TEXT, "分享的文本信息");
// 把intent放入到分享提供这种,有一个列表出来给你选择,然后将信息放入到你选择打开的应用中
provider.setShareIntent(shareintent);

}
}
//布局文件
<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"
android:gravity="center" >

<TextView
android:id="@+id/share_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="分享文本" />

<ImageView
android:id="@+id/share_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/share_text"
android:layout_below="@+id/share_text"
android:layout_marginTop="20dp"
android:src="@drawable/ic_launcher"
android:onClick="selectImage"
android:scaleType="centerInside" />

</RelativeLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: