美文网首页
Android系统分享功能

Android系统分享功能

作者: 星星326 | 来源:发表于2022-11-16 16:32 被阅读0次

    Android系统是自带分享功能的,不过也有一定的局限性,可以分享图片,文字,视频,音频等,也可以分享多图,但是不支持直接分享一个卡片(包括图文,链接),所以一般都是将需要分享的内容添加到图片中,或者通过整串文字的方式来分享。

    下边是几种分享方式的具体代码:

    action设为send或者send multiple,然后设置分享的类型和要分享内容

    分享文字

    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_SEND);
    intent.putExtra(Intent.EXTRA_TEXT, "文本内容");
    intent.setType("text/plain");
    startActivity(intent);
    

    分享图片

    private void shareImage(Bitmap bitmap) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(mActivity.getContentResolver(), bitmap, "IMG" + Calendar.getInstance().getTime(), null));
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        mActivity.startActivity(Intent.createChooser(intent, "title"));
    }
    

    分享多图

    ArrayList<Uri> imageUris = new ArrayList<>();
    imageUris.add(uri);
    imageUris.add(uri);
    Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
    shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
    shareIntent.setType("image/*");
    startActivity(Intent.createChooser(shareIntent, "dlgTitle"));
    

    分享到指定平台

    Intent wechatIntent = new Intent(Intent.ACTION_SEND);
    wechatIntent.setPackage("com.tencent.mm");
    wechatIntent.setType("text/plain");
    wechatIntent.putExtra(Intent.EXTRA_TEXT, "分享到微信的内容");
    startActivity(wechatIntent);
    

    常用的就是这几种方式,下边是支持的分享类型:

    text/plain
    application/*
    image/*
    video/*
    audio/*
    

    希望可以帮助到有相关需求的朋友

    Mark hahaha
    作者:lebronzhen
    链接:https://www.jianshu.com/p/31dc45a9cdab
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

    相关文章

      网友评论

          本文标题:Android系统分享功能

          本文链接:https://www.haomeiwen.com/subject/ltnyxdtx.html