MainActivity如下:

package cn.testshare1;import java.io.File;import java.util.ArrayList;import android.app.Activity;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;/** * Demo描述: * Android应用程序之间共享文字和图片 *  * 参考资料: * http://www.vmeitime.com/post/2012-06-08/40027373105 */public class MainActivity extends Activity {    private Button mTextButton;    private Button mPictureButton;    private Button mPicturesButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);init();}    private void init(){    mTextButton=(Button) findViewById(R.id.shareTextButton);    mTextButton.setOnClickListener(new ClickListenerImpl());    mPictureButton=(Button) findViewById(R.id.sharePicButton);    mPictureButton.setOnClickListener(new ClickListenerImpl());    mPicturesButton=(Button) findViewById(R.id.sharePicsButton);    mPicturesButton.setOnClickListener(new ClickListenerImpl());    }private class ClickListenerImpl implements OnClickListener {@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.shareTextButton:shareText("这是要分享的内容");break;case R.id.sharePicButton:sharePicture();break;case R.id.sharePicsButton:sharePictures();break;default:break;}}}//共享文字private void shareText(String string) {Intent intent = new Intent(Intent.ACTION_SEND);intent.putExtra(Intent.EXTRA_TEXT, string);intent.setType("text/plain");Intent.createChooser(intent,"共享文字");startActivity(intent);}//共享一张图片private void sharePicture(){Intent intent = new Intent();intent.setAction(Intent.ACTION_SEND);File file = new File(Environment.getExternalStorageDirectory()+File.separator+"test1.png");intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));intent.setType("image/jpeg");Intent.createChooser(intent, "共享一张图片");startActivity(intent);}//共享多张图片private void sharePictures(){ArrayList<Uri> picturesUriArrayList=new ArrayList<Uri>();File pictureFile1=new File(Environment.getExternalStorageDirectory()+File.separator+"test1.png");File pictureFile2=new File(Environment.getExternalStorageDirectory()+File.separator+"test2.png");Uri pictureUri1=Uri.fromFile(pictureFile1);Uri pictureUri2=Uri.fromFile(pictureFile2);//不是很好的方式://因为某些机型报错//Uri pictureUri1=Uri.parse("file:///mnt/sdcard/test1.png");//Uri pictureUri2=Uri.parse("file:///mnt/sdcard/test2.png");//错误的方式://Uri pictureUri1=Uri.parse//(Environment.getExternalStorageDirectory()+File.separator+"test1.png");//Uri pictureUri2=Uri.parse//(Environment.getExternalStorageDirectory()+File.separator+"test2.png");picturesUriArrayList.add(pictureUri1);picturesUriArrayList.add(pictureUri2);Intent intent = new Intent();intent.setAction(Intent.ACTION_SEND_MULTIPLE);    intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, picturesUriArrayList); intent.setType("image/jpeg");Intent.createChooser(intent, "共享多张图片");startActivity(intent);}}


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"    >    <Button        android:id="@+id/shareTextButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_marginTop="50dip"        android:text="共享文字"      />    <Button        android:id="@+id/sharePicButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_marginTop="150dip"        android:text="共享一张图片"      />    <Button        android:id="@+id/sharePicsButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_marginTop="250dip"        android:text="共享多张图片"      /></RelativeLayout>


更多相关文章

  1. android对大图片的缓存处理
  2. Android跳转系统相机或相册获取图片
  3. android中setImageResource加载大图片出现oom解决方案
  4. android 多点触摸 实现图片缩放 Image Zoom
  5. android 按钮的文字显示不全
  6. android 资料文档共享
  7. tabcontent中scaleType的使用 图片Drawable和bitmap显示问题
  8. Android学习记录使用Gallery实现炫丽的拖动效果
  9. Androd之在图片右上角显示红色圆圈里面数字提醒

随机推荐

  1. android 自定义通知
  2. Android获取网络状态
  3. Android模仿QQ空间图片上传——原理
  4. android动态显示图片
  5. android su源码
  6. Understanding some instructions from a
  7. [置顶] 54.EasyArcLoading
  8. ListView/Adapter IllegalStateException
  9. Android: Downloading the Google USB Dr
  10. Android(安卓)Bluedroid 结构