instagram、FaceBook分享


/**
 * 动态
 * https://developers.facebook.com/docs/instagram/sharing-to-feed
 * 快拍
 * https://developers.facebook.com/docs/instagram/sharing-to-stories
 */
fun Activity.shareIns(
    path: String
) { // Create the new Intent using the 'Send' action.
    val share = getInsShareIntent(path)
    // Broadcast the Intent.
    startActivity(share)
}
fun Fragment.shareIns(
    path: String
) { // Create the new Intent using the 'Send' action.
    val share = getInsShareIntent(path)
    // Broadcast the Intent.
    startActivity(share)
}
private fun getInsShareIntent(path: String): Intent {
    val share = Intent(Intent.ACTION_SEND)
    share.setPackage("com.instagram.android")//包含 动态 、快拍 、直接

    // Set the MIME type
    share.type = if (path.endsWith(".mp4")) {
        "video/*"
    } else "image/*"
    // Create the URI from the media
    // Add the URI to the Intent.
    share.putExtra(Intent.EXTRA_STREAM, UriUtils.file2Uri(File(path)))
    share.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    return share
}

/**
 * 需要网络畅通并且集成Facebook分享SDK
 * 分享到facebook 用户每次可以分享最多包含 6 个照片和视频元素的内容。
 * https://developers.facebook.com/docs/sharing/android
 */
private fun ShareDialog.share(
    imgs: List<String>?,
    videos: List<String>?,
    callbackManager: CallbackManager
) {
    val builder = ShareMediaContent.Builder()
    imgs?.forEach {
        adjustImage(it)?.let {
            val sharePhoto = SharePhoto.Builder()
                .setBitmap(it)
                .build()
            builder.addMedium(sharePhoto)
        }
    }

    videos?.forEach {
        val shareVideo1 = ShareVideo.Builder()
            .setLocalUrl(UriUtils.file2Uri(File(it)))
            .build()
        builder.addMedium(shareVideo1)
    }
    val shareContent = builder.build()

    registerCallback(
        callbackManager, object : FacebookCallback<Sharer.Result> {
            override fun onSuccess(result: Sharer.Result?) {
                "分享成功".toast()
                Log.i("facebookShare", result.toString())
            }

            override fun onCancel() {
                "分享失败".toast()
                Log.i("facebookShare", "cancel")
            }

            override fun onError(error: FacebookException?) {
                "分享失败".toast()
                Log.i("facebookShare", "onError")
                error?.printStackTrace()
            }

        })
    show(shareContent, ShareDialog.Mode.AUTOMATIC)
}

fun Activity.shareFacebook(
    imgs: List<String>?,
    videos: List<String>?,
    callbackManager: CallbackManager
) = ShareDialog(this).share(imgs, videos, callbackManager)

fun Fragment.shareFacebook(
    imgs: List<String>?,
    videos: List<String>?,
    callbackManager: CallbackManager
) = ShareDialog(this).share(imgs, videos, callbackManager)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值