//菜單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>