android 关于微信分享图片过大失败的解决方案。这是因为微信对缩略图做了限制,最大不超过32k。
所以可以先通过图片加载工具获取bitmap,然后进行压缩,压缩方法如下:
//压缩图片
public bitmap createbitmapthumbnail(bitmap bitmap) {
int width = bitmap.getwidth();
int height = bitmap.getheight();
// 设置想要的大小
int newwidth = 99;
int newheight = 99;
// 计算缩放比例
float scalewidth = ((float) newwidth) / width;
float scaleheight = ((float) newheight) / height;
// 取得想要缩放的matrix参数
matrix matrix = new matrix();
matrix.postscale(scalewidth, scaleheight);
// 得到新的图片
bitmap newbitmap = bitmap.createbitmap(bitmap, 0, 0, width, height, matrix, true);
return newbitmap;
}
然后封装成umusic对象进行音频分享:
final umusic music = new umusic(detail.path);//音频媒体对象
umimage umimage = new umimage(mactivity, bitmap);//将缩略图封装成umimage对象,在设置给音频媒体对象
music.settitle(detail.name);
music.setthumb(umimage);
new shareaction(mactivity).setplatform(media).setcallback(umsharelistener)//设置回调
.withtitle(detail.name)//标题
.withtext(getstring(r.string.share_text_pre) + detail.name + getstring(r.string.share_text_next))//描述信息
.withmedia(umusic)//媒体对象
.withtargeturl(url)//目标地址,就是点击分享要跳转的页面
.share();
分享视频:
string url = postsharecontent(mainurl, detail);//url点击跳转页面地址
umvideo image = new umvideo(url);
image.settitle(detail.name);
image.setthumb(detail.coverpath);//这里如果图片过大也应该和音频一样,
log.log = false;
new shareaction(mactivity).setplatform(media).setcallback(umsharelistener)
.withtitle(detail.name)
.withtext(getstring(r.string.share_text_pre) + detail.name + getstring(r.string.share_text_next))
.withmedia(image)
// .withtargeturl(url)//地址封装在umvideo中,所以不必调用此方法
.share();
loggerutils.d(tag + "\n" + string.format(" 分享地址 :%s \n 分享标题 :%s \n 分享图标 :%s \n 分享音频 :%s",
url,detail.name,detail.coverpath,detail.path));
分享图片:
umimage image = new umimage(mactivity, detail.path);
image.settitle(detail.name);
image.setthumb(detail.coverpath);
string url = postsharecontent(mainurl, detail);
log.log = false;
// config.istoasttip = true;
new shareaction(mactivity).setplatform(media).setcallback(umsharelistener)
.withtitle(detail.name)
.withtext(getstring(r.string.share_text_pre) + detail.name + getstring(r.string.share_text_next))
.withmedia(image)
.withtargeturl(url)
.share();
最后接口回调类:
/**
* 分享回调监听
*/
private umsharelistener umsharelistener = new umsharelistener() {
@override
public void onresult(share_media platform) {
loggerutils.d("share result");
}
@override
public void onerror(share_media platform, throwable t) {
if (t != null) {
loggerutils.d(t.getmessage());
}
}
@override
public void oncancel(share_media platform) {
loggerutils.d("share cancel");
}
};