ActionBar中ShareActionProvider简单使用

//功能:点击中间机器人图标打开图库 然后选中要分享的图片

//在点击选择菜单分享图标 分享到需要分享的地方


效果示例图:


195400_TPy3_2542711.png






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);

}*/

}


转载于:https://my.oschina.net/u/2542711/blog/613494

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值