//功能:點擊中間機器人圖標打開圖庫 而後選中要分享的圖片
android
//在點擊選擇菜單分享圖標 分享到須要分享的地方ide
效果示例圖:佈局
一、選擇菜單 share_menu.xml 佈局this
代碼spa
<?xml version="1.0" encoding="utf-8"?>code
<menu xmlns:android="http://schemas.android.com/apk/res/android" >xml
<item事件
android:id="@+id/actionprovider"圖片
android:actionProviderClass="android.widget.ShareActionProvider"utf-8
android:showAsAction="always"
android:title=""/>
</menu>
===================
二、主佈局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>
==============
三、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);
}*/
}