Android Intent 跳转 发送朋友圈

来源:互联网 发布:mac如何设置桌面图标 编辑:程序博客网 时间:2024/04/28 22:51

写在前面

2016年7 月份 微信封了几乎所有的deeplink URL Scheme ,现在基本上不可以直接通过deeplink 跳转到微信去执行相应的操作,但是很多时候我们还是不希望集成 SDK去获取接口,毕竟既有65535 方法数限制,又有引包麻烦,更新迭代麻烦等相应问题,我尝试着 使用传统的直接Intent 调相应的action 执行相关操作。

问题原因:

网上很多相关的教程,但是在我这边测试使用时出现了问题,比如说 可以发送 文字·图片等到微信好友,这一点没有问题,但是想要去发送照片到朋友圈,我们发现,很难直接做到这一步操作,通过查看反编译微信得到的manifest 文件,我们看出 发送到朋友圈这个Activity只接受 image/* 类型的文件,当我们尝试发布本地图片或者网络图片时,又出现了问题,它只识别 content// 类型的URI,而且,我们将URI转换成 相应的URI时 还是获取不到相应的图片资源,没办法只有使用笨方法:

具体方法:

1.先将生成的图文 存成图片 存入图库
2.再通知系统更新图片信息
3.获取图库中相应图片的URI,调起 微信相应activity
4.发布朋友圈

具体的方法 见 代码:

                Resources res=getResources();                Bitmap bmp= BitmapFactory.decodeResource(res, R.drawable.main_activity_list_1);                String filePrefix= null;                try {                    filePrefix = saveImageToGallery(this,bmp);                } catch (IOException e) {                    e.printStackTrace();                }                ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");                intent.setComponent(comp);                intent.putExtra(Intent.EXTRA_SUBJECT, "分享");                intent.putExtra(Intent.EXTRA_TEXT, "你好 ");                intent.putExtra(Intent.EXTRA_TITLE, "我是标题");                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                    Uri uri=Uri.parse(filePrefix);                    Log.i("uri", "" + uri.getScheme());                    intent.setAction(Intent.ACTION_SEND);                    intent.setType("image");                    Log.i("image", " " + uri);//这里做一下修改可以发多图////                ArrayList<Uri> imageUris = new ArrayList<>();////                    imageUris.add(uri);//                    intent.putExtra(Intent.EXTRA_STREAM, uri);                startActivity(intent);

相关工具类:

  public static String saveImageToGallery(Context context, Bitmap bmp) throws IOException {        // 首先保存图片        File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee");        if (!appDir.exists()) {            appDir.mkdir();        }        String fileName = System.currentTimeMillis()+".jpg";        String fileNameTemp;        File file = new File(appDir, fileName);        file.createNewFile();        try {            FileOutputStream fos = new FileOutputStream(file);            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);            fos.flush();            fos.close();        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        // 其次把文件插入到系统图库        try {            fileNameTemp=MediaStore.Images.Media.insertImage(context.getContentResolver(),                    file.getAbsolutePath(), fileName, null);        } catch (FileNotFoundException e) {            e.printStackTrace();            fileNameTemp="";        }        // 最后通知图库更新        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(Environment.getExternalStorageDirectory().toString()+"/Boohee")));        return fileNameTemp;//返回的就是 content://开头的具体地址    }

同时分享一些成功使用没有大费周章的方法:

  • 调用支付宝扫码 支付 (可用于向开发者捐款项目)
 Uri uriPay = Uri.parse("alipayqr://platformapi/startapp?saId=10000007&qrcode=你的二维码对应的网址");intent = new Intent(Intent.ACTION_VIEW, uriPay);

qq的deepLink

//String url = "mqqwpa://im/chat?chat_type=group&uin=463028**3&version=1";//这是调到指定的qq群String url="mqqwpa://im/chat?chat_type=wpa&uin=XXXXXXXX";startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));//这是调起跟个人的聊天儿

希望 能对你有帮助
github【https://github.com/panyunyi97】

原创粉丝点击