首页 > Android > 笔记 > 正文

安卓调用系统自带分享功能分享文字,分享大图片,仿好奇心日报分享长图片(不用申请微信微博官方sdk就能直接分享)

2018-07-22 笔记 3807 ℃ 0 评论

当前安卓分享一共三种方式
1,调用安卓系统自带分享功能(可以分享大图)
2,调用微信,微博支付宝等自己的官方分享sdk
3,用友盟,shareSdk等集成好的sdk


由于公司业务要求,需要分享一些长图,大图到微信,微博等。由于微信微博自己的官方sdk对图片有限制,
比如微博要求缩略图不能大于32k,原图不能大于2M.所以今天来给大家讲下不通过任何sdk,直接调用安卓系统自带的分享功能


安卓系统自带的分享功能可以分享文字,长图片,下面就分别给大家讲解下


一,系统自带分享文字

Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        intent.putExtra(Intent.EXTRA_SUBJECT, "标题");
        intent.putExtra(Intent.EXTRA_TEXT, "描述信息" + "这里你可以追加一个url连接");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(Intent.createChooser(intent, "分享到"));

二,系统自带分享大图片之最简单的是直接分享本地图

String path = Environment.getExternalStorageDirectory() + File.separator+;//sd根目录
File file = new File(path, "share" + ".jpg");//这里share.jpg是sd卡根目录下的一个图片文件
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, "分享图片"));

三,通常我们分享的图片不可能是本地存好的图片,我们通常分享的是自己生成的图片获者网络图片
这里简单给大家介绍一个仿好奇心日报那样的把本地布局截图分享出去

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, "分享图片"));

转自https://blog.csdn.net/qiushi_1990/article/details/53611448

猜你喜欢

日历
«    2024年3月    »
123
45678910
11121314151617
18192021222324
25262728293031
标签列表
最近发表
友情链接