当前位置: 代码迷 >> Android >> android利用Intent.ACTION_SEND实现容易分享功能
  详细解决方案

android利用Intent.ACTION_SEND实现容易分享功能

热度:9   发布时间:2016-04-28 02:28:25.0
android利用Intent.ACTION_SEND实现简单分享功能


android中,利用Intent.ACTION_SEND可以实现简单“分享”功能,可以分享文字、图片等到其他应用,像微信、QQ、短信等。

MainActivity.java文件:

package com.example.androidtest;import java.io.File;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.app.Activity;import android.content.Intent;import android.view.Menu;public class MainActivity extends Activity {	@Override	protected void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.activity_main);		String strDlgTitle = "对话框标题 - 分享文字";		String strSubject = "我的主题";		String strContent = "我的分享内容";		/**		 * 1.分享纯文字内容		 *///		shareText(strDlgTitle, strSubject, strContent);		/**		 * 2.分享图片和文字内容		 */		strDlgTitle = "对话框标题 - 分享图片";		// 图片文件路径(SD卡根目录下“1.png”图片)		String imgPath = Environment.getExternalStorageDirectory().getPath()				+ File.separator + "1.png";		// 图片URI		Uri imageUri = Uri.fromFile(new File(imgPath));		// 分享		shareImg(strDlgTitle, strSubject, strContent, imageUri);	}	/**	 * 分享文字内容	 * 	 * @param dlgTitle	 *            分享对话框标题	 * @param subject	 *            主题	 * @param content	 *            分享内容(文字)	 */	private void shareText(String dlgTitle, String subject, String content) {		if (content == null || "".equals(content)) {			return;		}		Intent intent = new Intent(Intent.ACTION_SEND);		intent.setType("text/plain");		if (subject != null && !"".equals(subject)) {			intent.putExtra(Intent.EXTRA_SUBJECT, subject);		}		intent.putExtra(Intent.EXTRA_TEXT, content);		// 设置弹出框标题		if (dlgTitle != null && !"".equals(dlgTitle)) { // 自定义标题			startActivity(Intent.createChooser(intent, dlgTitle));		} else { // 系统默认标题			startActivity(intent);		}	}	/**	 * 分享图片和文字内容	 * 	 * @param dlgTitle	 *            分享对话框标题	 * @param subject	 *            主题	 * @param content	 *            分享内容(文字)	 * @param uri	 *            图片资源URI	 */	private void shareImg(String dlgTitle, String subject, String content,			Uri uri) {		if (uri == null) {			return;		}		Intent intent = new Intent(Intent.ACTION_SEND);		intent.setType("image/*");		intent.putExtra(Intent.EXTRA_STREAM, uri);		if (subject != null && !"".equals(subject)) {			intent.putExtra(Intent.EXTRA_SUBJECT, subject);		}		if (content != null && !"".equals(content)) {			intent.putExtra(Intent.EXTRA_TEXT, content);		}		// 设置弹出框标题		if (dlgTitle != null && !"".equals(dlgTitle)) { // 自定义标题			startActivity(Intent.createChooser(intent, dlgTitle));		} else { // 系统默认标题			startActivity(intent);		}	}	@Override	public boolean onCreateOptionsMenu(Menu menu) {		// Inflate the menu; this adds items to the action bar if it is present.		getMenuInflater().inflate(R.menu.activity_main, menu);		return true;	}}

其中,shareText方法实现了分享文本内容的功能,而shareImg方法实现了分享图片的功能。Intent.EXTRA_STREAM还可以分享其他类型的资源文件,比如:MP3。


运行效果如下所示:

1. 分享文字内容到微信

图1图2


图3图4


图5图6

2. 分享文字内容到QQ

图8图9图10

3. 分享图片到微信

图11图12


图13图14

4.分享图片到QQ

图15图16



  相关解决方案