分享功能的实现方式
2017-02-08 11:00
120 查看
1.通过intent调用系统中自带的分享功能。
//分享文字
publicvoid shareText(View view) {
Intent shareIntent = newIntent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "描述信息" + "这里你可以追加一个url连接"););
shareIntent.setType("text/plain");
//设置分享列表的标题,并且每次都显示分享列表
startActivity(Intent.createChooser(shareIntent,"分享到"));
}
//分享单张图片
publicvoid shareSingleImage(View view) {
String imagePath = Environment.getExternalStorageDirectory() + File.separator + "test.jpg";
//由文件得到uri
Uri imageUri = Uri.fromFile(newFile(imagePath));
Log.d("share","uri:"+ imageUri); //输出:file:///storage/emulated/0/test.jpg
Intent shareIntent = newIntent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent,"分享到"));
}
//分享多张图片
publicvoid shareMultipleImage(View view) {
ArrayList<uri> uriList = newArrayList<>();
String path = Environment.getExternalStorageDirectory() + File.separator;
uriList.add(Uri.fromFile(newFile(path+"australia_1.jpg")));
uriList.add(Uri.fromFile(newFile(path+"australia_2.jpg")));
uriList.add(Uri.fromFile(newFile(path+"australia_3.jpg")));
Intent shareIntent = newIntent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent,"分享到"));
}
通常我们分享的图片不可能是本地存好的图片,我们通常分享的是自己生成的图片获者网络图片
这里简单给大家介绍一个仿好奇心日报那样的把本地布局截图分享出去
1,首先获取截图
/*
* 将布局转化为bitmap
这里传入的是你要截的布局的根View
* */
public Bitmap getBitmapByView(View headerView) {
int h = headerView.getHeight();
Bitmap bitmap = Bitmap.createBitmap(headerView.getWidth(), h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
headerView.draw(canvas);
return bitmap;
}
2,把截图获取的bitmap做简单的压缩
/*
* 压缩图片
* */
private Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 10, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 100;
while (baos.toByteArray().length / 1024 > 400) { //循环判断如果压缩后图片是否大于400kb,大于继续压缩(这里可以设置大些)
baos.reset();//重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
options -= 10;//每次都减少10
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片
return bitmap;
}
3,把压缩过的图片先保存到本地才能调用系统分享出去,因为系统分享的是一个uri,我们需要先把bitmap转为本地file文件
再把file转换为uri
/*
* 把bitmap转化为file
* */
public File bitMap2File(Bitmap bitmap) {
String path = "";
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
path = Environment.getExternalStorageDirectory() + File.separator;//保存到sd根目录下
}
// File f = new File(path, System.currentTimeMillis() + ".jpg");
File f = new File(path, "share" + ".jpg");
if (f.exists()) {
f.delete();
}
try {
FileOutputStream out = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
bitmap.recycle();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
return f;
}
}
4,调用上面的方法获取到file,转换为uri并分享出去
File file = bitMap2File(compressImage);
if (file != null && file.exists() && file.isFile()) {
//由文件得到uri
Uri imageUri = Uri.fromFile(file);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享图片"));
}
运用ComponentName来指定分享到哪里
1,指定是分享到微信好友
String imagePath = Environment.getExternalStorageDirectory() + File.separator +
"huxiu.jpg";
//由文件得到uri
Uri imageUri = Uri.fromFile(new File(imagePath));
Intent shareIntent = new Intent();
//发送图片给好友。
ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
shareIntent.setComponent(comp);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享图片"));
2,指定分享到朋友圈
String imagePath = Environment.getExternalStorageDirectory() + File.separator +
"huxiu.jpg";
//由文件得到uri
Uri imageUri = Uri.fromFile(new File(imagePath));
Intent shareIntent = new Intent();
//发送图片到朋友圈
Com
b214
ponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
shareIntent.setComponent(comp);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享图片"));
3,指定分享到qq
String imagePath = Environment.getExternalStorageDirectory() + File.separator +
"huxiu.jpg";
//由文件得到uri
Uri imageUri = Uri.fromFile(new File(imagePath));
Intent shareIntent = new Intent();
//发送图片到qq
ComponentName comp = new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
shareIntent.setComponent(comp);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享图片"));
Intent share = new Intent(android.content.Intent.ACTION_SEND);
PackageManager packageManager = getPackageManager();
List<ResolveInfo> list=packageManager.queryIntentActivities(share, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
for(ResolveInfo info:list){
MyUtils.log(""+info.activityInfo.packageName+"---"+info.activityInfo.name);
}
遍历获取包名和类名:
// ("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");//微信朋友
// ("com.tencent.mobileqq", "cooperation.qqfav.widget.QfavJumpActivity");//保存到QQ收藏
// ("com.tencent.mobileqq", "cooperation.qlink.QlinkShareJumpActivity");//QQ面对面快传
// ("com.tencent.mobileqq", "com.tencent.mobileqq.activity.qfileJumpActivity");//传给我的电脑
("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");//QQ好友或QQ群
// ("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");//微信朋友圈,仅支持分享图片
功能不够强大
2.去各个平台注册自己的app,使用平台的分享协议
这种方式分享的内容丰富,而且弹出的分享界面正规无广告
3.直接使用第三方的ShareSdk重的分享(onekeyshare)
分享界面带有sharesdk的表示,很尴尬,但是简单
//分享文字
publicvoid shareText(View view) {
Intent shareIntent = newIntent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "描述信息" + "这里你可以追加一个url连接"););
shareIntent.setType("text/plain");
//设置分享列表的标题,并且每次都显示分享列表
startActivity(Intent.createChooser(shareIntent,"分享到"));
}
//分享单张图片
publicvoid shareSingleImage(View view) {
String imagePath = Environment.getExternalStorageDirectory() + File.separator + "test.jpg";
//由文件得到uri
Uri imageUri = Uri.fromFile(newFile(imagePath));
Log.d("share","uri:"+ imageUri); //输出:file:///storage/emulated/0/test.jpg
Intent shareIntent = newIntent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent,"分享到"));
}
//分享多张图片
publicvoid shareMultipleImage(View view) {
ArrayList<uri> uriList = newArrayList<>();
String path = Environment.getExternalStorageDirectory() + File.separator;
uriList.add(Uri.fromFile(newFile(path+"australia_1.jpg")));
uriList.add(Uri.fromFile(newFile(path+"australia_2.jpg")));
uriList.add(Uri.fromFile(newFile(path+"australia_3.jpg")));
Intent shareIntent = newIntent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent,"分享到"));
}
通常我们分享的图片不可能是本地存好的图片,我们通常分享的是自己生成的图片获者网络图片
这里简单给大家介绍一个仿好奇心日报那样的把本地布局截图分享出去
1,首先获取截图
/*
* 将布局转化为bitmap
这里传入的是你要截的布局的根View
* */
public Bitmap getBitmapByView(View headerView) {
int h = headerView.getHeight();
Bitmap bitmap = Bitmap.createBitmap(headerView.getWidth(), h, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
headerView.draw(canvas);
return bitmap;
}
2,把截图获取的bitmap做简单的压缩
/*
* 压缩图片
* */
private Bitmap compressImage(Bitmap image) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 10, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
int options = 100;
while (baos.toByteArray().length / 1024 > 400) { //循环判断如果压缩后图片是否大于400kb,大于继续压缩(这里可以设置大些)
baos.reset();//重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
options -= 10;//每次都减少10
}
ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片
return bitmap;
}
3,把压缩过的图片先保存到本地才能调用系统分享出去,因为系统分享的是一个uri,我们需要先把bitmap转为本地file文件
再把file转换为uri
/*
* 把bitmap转化为file
* */
public File bitMap2File(Bitmap bitmap) {
String path = "";
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
path = Environment.getExternalStorageDirectory() + File.separator;//保存到sd根目录下
}
// File f = new File(path, System.currentTimeMillis() + ".jpg");
File f = new File(path, "share" + ".jpg");
if (f.exists()) {
f.delete();
}
try {
FileOutputStream out = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
bitmap.recycle();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
return f;
}
}
4,调用上面的方法获取到file,转换为uri并分享出去
File file = bitMap2File(compressImage);
if (file != null && file.exists() && file.isFile()) {
//由文件得到uri
Uri imageUri = Uri.fromFile(file);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享图片"));
}
运用ComponentName来指定分享到哪里
1,指定是分享到微信好友
String imagePath = Environment.getExternalStorageDirectory() + File.separator +
"huxiu.jpg";
//由文件得到uri
Uri imageUri = Uri.fromFile(new File(imagePath));
Intent shareIntent = new Intent();
//发送图片给好友。
ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
shareIntent.setComponent(comp);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享图片"));
2,指定分享到朋友圈
String imagePath = Environment.getExternalStorageDirectory() + File.separator +
"huxiu.jpg";
//由文件得到uri
Uri imageUri = Uri.fromFile(new File(imagePath));
Intent shareIntent = new Intent();
//发送图片到朋友圈
Com
b214
ponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
shareIntent.setComponent(comp);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享图片"));
3,指定分享到qq
String imagePath = Environment.getExternalStorageDirectory() + File.separator +
"huxiu.jpg";
//由文件得到uri
Uri imageUri = Uri.fromFile(new File(imagePath));
Intent shareIntent = new Intent();
//发送图片到qq
ComponentName comp = new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
shareIntent.setComponent(comp);
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享图片"));
Intent share = new Intent(android.content.Intent.ACTION_SEND);
PackageManager packageManager = getPackageManager();
List<ResolveInfo> list=packageManager.queryIntentActivities(share, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
for(ResolveInfo info:list){
MyUtils.log(""+info.activityInfo.packageName+"---"+info.activityInfo.name);
}
遍历获取包名和类名:
// ("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");//微信朋友
// ("com.tencent.mobileqq", "cooperation.qqfav.widget.QfavJumpActivity");//保存到QQ收藏
// ("com.tencent.mobileqq", "cooperation.qlink.QlinkShareJumpActivity");//QQ面对面快传
// ("com.tencent.mobileqq", "com.tencent.mobileqq.activity.qfileJumpActivity");//传给我的电脑
("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");//QQ好友或QQ群
// ("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");//微信朋友圈,仅支持分享图片
功能不够强大
2.去各个平台注册自己的app,使用平台的分享协议
这种方式分享的内容丰富,而且弹出的分享界面正规无广告
3.直接使用第三方的ShareSdk重的分享(onekeyshare)
分享界面带有sharesdk的表示,很尴尬,但是简单
相关文章推荐
- ASP.NET MVC使用中转站方式获取code实现微信登录,分享功能
- android 实现分享功能
- [PHP] 原生PHP使用递归方式实现数组转换成XML的功能之思考
- iOS开发实现文字/图片的社交网络分享功能
- Android开发之使用SharedPreferences实现QQ登陆的选项框记忆功能(源代码分享)
- IOS项目集成ShareSDK实现第三方登录、分享、关注等功能。
- Powershell实现获取电脑序列号功能脚本分享
- iOS截屏分享功能实现
- 根据ShareSDK实现分享功能组件化
- Android中“分享”功能的实现
- android APP 中微信分享功能实现 的总结
- 使用AppCan实现分享网站功能
- XP中完美实现vista中快捷方式右键添加“打开文件位置”功能
- Android实现新浪微博SSO授权登录分享文字图片等功能
- 如何使用友盟 Cocos2d-x 分享组件实现 Android/iOS 分享功能
- 两种方式实现checkBox readonly功能
- shell脚本实现ssh自动登录功能分享
- Android实现分享图片和文字的功能
- js实现分享功能
- jquery实现邮箱自动补全功能示例分享