标签: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;
}
}
运行效果如下所示:
1. 分享文字内容到微信
2. 分享文字内容到QQ
3. 分享图片到微信
4.分享图片到QQ
android利用Intent.ACTION_SEND实现简单分享功能
标签:android intent 分享 action_send
原文地址:http://blog.csdn.net/chadeltu/article/details/43450713