资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

android7.0实现分享图片到朋友圈功能-创新互联

本文实例为大家分享了android实现分享图片到朋友圈功能的具体代码,供大家参考,具体内容如下

成都创新互联公司成立于2013年,是专业互联网技术服务公司,拥有项目网站制作、网站建设网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元汨罗做网站,已为上家服务,为汨罗各地企业和个人服务,联系电话:18980820575

在Android7.0中,系统对scheme为file://的uri进行了限制,所以通过这种uri来进行分享的一些接口就不能用了,比如使用代码来调用分享朋友圈的接口。

此时就得使用其他的URI scheme来代替 file://,比如MediaStore的 content://。直接上代码:

private static boolean checkInstallation(Context context, String packageName) {
  try {
   context.getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
   return true;
  } catch (PackageManager.NameNotFoundException e) {
   return false;
  }
 }

 public static void shareToWeChat(View view, Context context) {
  // TODO: 2015/12/13 将需要分享到微信的图片准备好
  try {
   if (!checkInstallation(context, "com.tencent.mm")) {
    SnackBarUtil.show(view, R.string.share_no_wechat);
    return;
   }
   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/*");
//  intent.setType("text/plain");
   //添加Uri图片地址
//  String msg=String.format(getString(R.string.share_content), getString(R.string.app_name), getLatestWeekStatistics() + "");
   String msg = context.getString(R.string.share_content);
   intent.putExtra("Kdescription", msg);
   ArrayList imageUris = new ArrayList();
   // TODO: 2016/3/8 根据不同图片来设置分享
   File dir = context.getExternalFilesDir(null);
   if (dir == null || dir.getAbsolutePath().equals("")) {
    dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
   }
   File pic = new File(dir, "bigbang.jpg");
   pic.deleteOnExit();
   BitmapDrawable bitmapDrawable;
   if (Build.VERSION.SDK_INT < 22) {
    bitmapDrawable = (BitmapDrawable) context.getResources().getDrawable(R.mipmap.bannar);
   } else {
    bitmapDrawable = (BitmapDrawable) context.getDrawable(R.mipmap.bannar);
   }
   try {
    bitmapDrawable.getBitmap().compress(Bitmap.CompressFormat.JPEG, 75, new FileOutputStream(pic));
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   }
   if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
    imageUris.add(Uri.fromFile(pic));
   }else {
    //修复微信在7.0崩溃的问题
    Uri uri =Uri.parse(android.provider.MediaStore.Images.Media.insertImage(context.getContentResolver(), pic.getAbsolutePath(), "bigbang.jpg", null));
    imageUris.add(uri);
   }

   intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
   ((Activity) context).startActivityForResult(intent, 1000);
  }catch (Throwable e){
   SnackBarUtil.show(view,R.string.share_error);
 }


网页题目:android7.0实现分享图片到朋友圈功能-创新互联
新闻来源:http://cdkjz.cn/article/hcjgs.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220