/**
* 动态
* 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)