Android使用Intent一键分享图片文字到腾讯、新浪、开心、微信等
16lz
2021-01-24
现在分享应用很多,消耗研发与维护时间,增加项目成本,而且集成分享功能后我们的应用包会比以前要大几MB。其实有android的Intent可以满足分享文字和图片的需求,而且不增加应用包的大小。
/** * 判断是否安装腾讯、新浪等指定的分享应用 * @param packageName 应用的包名 */public boolean checkInstallation(String packageName){ try { this.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES); return true; } catch (NameNotFoundException e) { return false; }}
如果没有安装指定的应用,下面代码引导跳转去Google Play上某个应用的详细页面
Uri uri = Uri.parse("market://details?id=应用包名");Intent it = new Intent(Intent.ACTION_VIEW,uri);startActivity(it);
用下面代码可以弹出一个分享列表让用户选择需要分享哪些文字内容。如果去调用setPackage方法的注销就会直接跳转新浪微博分享文字。 Intent intent=new Intent(Intent.ACTION_SEND); intent.setType("text/plain");//intent.setPackage("com.sina.weibo"); intent.putExtra(Intent.EXTRA_SUBJECT, "分享"); intent.putExtra(Intent.EXTRA_TEXT, "你好 ");intent.putExtra(Intent.EXTRA_TITLE, "我是标题");intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(Intent.createChooser(intent, "请选择"));
微信分享多张图片
Intent intent = new Intent();//分享精确到微信的页面,朋友圈页面,或者选择好友分享页面ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");intent.setComponent(comp);intent.setAction(Intent.ACTION_SEND_MULTIPLE);intent.setType("image/*");//添加Uri图片地址ArrayList imageUris = new ArrayList();intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);startActivity(intent);
分享单张图片,操作与分享文字类似。
Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.setType("image/*"); Uri uri = Uri.fromFile(getFileStreamPath("shared.png")); shareIntent.putExtra(Intent.EXTRA_STREAM, uri);//startActivity(Intent.createChooser(shareIntent, "请选择"));
用Intent分享多张图片
/** * 分享多张照片 */private void sendMultiple(){Intent intent=new Intent(Intent.ACTION_SEND_MULTIPLE);intent.setType("image/*");intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, getUriListForImages());intent.putExtra(Intent.EXTRA_SUBJECT, "分享");intent.putExtra(Intent.EXTRA_TEXT, "你好 ");intent.putExtra(Intent.EXTRA_TITLE, "我是标题");intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(Intent.createChooser(intent, "请选择")); }/** * 设置需要分享的照片放入Uri类型的集合里 */private ArrayList getUriListForImages() { ArrayList myList = new ArrayList(); String imageDirectoryPath = Environment.getExternalStorageDirectory().getAbsolutePath()+ "/DCIM/100ANDRO/"; File imageDirectory = new File(imageDirectoryPath); String[] fileList = imageDirectory.list(); if(fileList.length != 0) { for(int i=0; i<5; i++){ try{ ContentValues values = new ContentValues(7); values.put(Images.Media.TITLE, fileList[i]); values.put(Images.Media.DISPLAY_NAME, fileList[i]); values.put(Images.Media.DATE_TAKEN, new Date().getTime()); values.put(Images.Media.MIME_TYPE, "image/jpeg"); values.put(Images.ImageColumns.BUCKET_ID, imageDirectoryPath.hashCode()); values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, fileList[i]); values.put("_data", imageDirectoryPath + fileList[i]); ContentResolver contentResolver = getApplicationContext().getContentResolver(); Uri uri = contentResolver.insert(Images.Media.EXTERNAL_CONTENT_URI, values); myList.add(uri); } catch (Exception e) { e.printStackTrace(); } } } return myList; }
作者:黑卡米 原文地址:http://blog.csdn.net/aaren_jiang/article/details/8660538
更多相关文章
- Android+Jquery Mobile学习系列-目录
- Android(安卓)通过selector改变状态
- android修改桌面底端快捷图片为Settings
- 【Android】读取sdcard上的图片
- 画图技巧
- android图片叠加方法
- android 图片阅读 之 穹の思念
- 【android】scaleType属性与ImagView中图片的显示的关系
- 重拾Android