分享功能的实现方式
来源:互联网 发布:小学生上网数据 编辑:程序博客网 时间:2024/05/01 16:33
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();
//发送图片到朋友圈
ComponentName 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的表示,很尴尬,但是简单
0 0
- 分享功能的实现方式
- android分享功能的实现
- android分享功能的实现
- android分享功能的实现
- android 分享功能的实现
- Android 分享功能的实现
- Android 分享功能的实现
- Android分享功能的实现
- Android中“分享”功能的实现
- Android中“分享”功能的实现
- Android中“分享”功能的实现
- Android中“分享”功能的实现
- Android中“分享”功能的实现
- Android中“分享”功能的实现
- Android中“分享”功能的实现
- Android系统中分享功能的实现
- Android中“分享”功能的实现
- android中实现应用程序的分享功能
- Java开发岗位面试题归类
- 设计模式——观察者模式(Observer)
- Linux基础知识和常用命令(三)
- zabbix监控端口详解
- test1
- 分享功能的实现方式
- 记录从greendao2.1升级到greendao3.2!
- JAVASE基础-day21(IO(字符流)&字符流其他内容&递归)
- 记录一下解决JRE System Library(nobound)的问题
- IBM DB2 Alphablox 8.3 build 149 安装过程 部署 WebSphere Application Server 6.0.1上
- npm用法及离线安装方法
- #POJ1601#青蛙的约会
- Github的使用技巧
- 设计模式——门面模式(Facade)