短视频开发,分享功能占据着重要的地位,主流的社交APP进行分享是一种很常见的推广、使用场景,微博是被分享的众多渠道之一,下面介绍一下,短视频开发Android APP是如何接入微博分享的。 首先,进行微博开发者平台的应用申请等操作,获取APPKEY等信息,然后,将微博SDK引入依赖。 接下来,介绍相关的代码操作: 在相关的Activity中,进行SDK的初始化
private static final String APP_KY = " Key";
private static final String REDIRECT_URL = "url";
private static final String SCOPE = "scope";
private IWBAPI mWBAPI;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//init sdk
initSdk();
}
private void initSdk() {
AuthInfo authInfo = new AuthInfo(this, APP_KY, REDIRECT_URL, SCOPE);
mWBAPI = WBAPIFactory.createWBAPI(this);
mWBAPI.registerApp(this, authInfo);
}
使用授权
private void startAuth() {
//auth
mWBAPI.authorize(new WbAuthListener() {
@Override
public void onComplete(Oauth2AccessToken token) {
Toast.makeText(MainActivity.this, "微博授权成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(UiError error) {
Toast.makeText(MainActivity.this, "微博授权出错", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel() {
Toast.makeText(MainActivity.this, "微博授权取消", Toast.LENGTH_SHORT).show();
}
});
}
//使用微博客户端授权
private void startClientAuth() {
mWBAPI.authorizeClient(new WbAuthListener() {
@Override
public void onComplete(Oauth2AccessToken token) {
Toast.makeText(MainActivity.this, "微博授权成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(UiError error) {
Toast.makeText(MainActivity.this, "微博授权出错", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel() {
Toast.makeText(MainActivity.this, "微博授权取消",
Toast.LENGTH_SHORT).show();
}
});
}
重写onActivityResult 获取微博授权的回调@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (mWBAPI != null) {
mWBAPI.authorizeCallback(requestCode, resultCode, data);
}
}
// 分享 回调
private static class ShareCallback implements WbShareCallback {
@Override
public void onComplete() {
Toast.makeText(ShareActivity.this, "????", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(UiError error) {
Toast.makeText(ShareActivity.this, "????:" + error.errorMessage, Toast.LENGTH_SHORT).show();
}
@Override
public void onCancel() {
Toast.makeText(ShareActivity.this, "????", Toast.LENGTH_SHORT).show();
}
}
//?通过微博客户端授权操作
private void doWeiboShare() {
WeiboMultiMessage message = new WeiboMultiMessage();
TextObject textObject = new TextObject();
String text = "分享文本?";
分享文字
if (mShareText.isChecked()) {
text = "分享文本";
}
textObject.text = text;
message.textObject = textObject;
// 分享单图
if (mShareImage.isChecked()) {
ImageObject imageObject = new ImageObject();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.share_image);
imageObject.setImageData(bitmap);
message.imageObject = imageObject;
}
// 分享网页链接
if (mShareUrl.isChecked()) {
WebpageObject webObject = new WebpageObject();
webObject.identify = UUID.randomUUID().toString();
webObject.title = "标题";
webObject.description = "描述";
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_logo);
ByteArrayOutputStream os = null;
try {
os = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, os);
webObject.thumbData = os.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(os!=null){
os.close();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
webObject.actionUrl="https://weibo.com";
webObject.default
Text="分享网页";
message.mediaObject=webObject;
}
/ /分享多图
分享多图,短视频开发需要设置的是 本地图片文件的路径,并且是当前应用可访问的路径(高版本要申请读写权限),目前无法设置网络图片
if (mShareMultiImage.isChecked()) {
MultiImageObject multiImageObject = new MultiImageObject();
List<Uri> list = new ArayList<Uri>()?
list.add(Uri.fromFile(new File(“图片文件路径”)));
list.add(Uri.fromFile(new File(“图片文件路径”)));
list.add(Uri.fromFile(new File(“图片文件路径”)));
list.add(Uri.fromFile(new File(“图片文件路径”)));
list.add(Uri.fromFile(new File(“图片文件路径”)));
list.add(Uri.fromFile(new File(“图片文件路径”)));
multiImageObject.imageList = list;
message.multiImageObject = multiImageObject;
}
// 分享视频
分享视频,短视频开发需要设置的是 本地视频文件的路径,并且是当前应用可访问的路径(高版本要申请读写权限),目前无法设置网络视频链接,目前仅支持MP4格式
if (mShareVideo.isChecked()) {
VideoSourceObject videoObject = new VideoSourceObject();
videoObject.videoPath = Uri.fromFile(new File(getExternalFilesDir(null) + "/eeee.mp4"));
message.videoSourceObject = videoObject;
}
// mShareClientOnly.isChecked() ?//标识是否只通过微博客户端分享
mWBAPI.shareMessage(message, mShareClientOnly.isChecked());
}
以上,就是短视频开发如何通过简单的微博sdk,进行微博分享的操作实现了,如果自定义分享内容,可以根据业务需求更改部分代码。 声明:以上内容为云豹科技作者本人原创,未经作者本人同意,禁止转载,否则将追究相关法律责任
|