最近再分享视频的时候出现一个问题缩略图不显示有些显示,虽然以前也遇到过但是忘了。今天这里再写一下方便记录
百度了一下很多!!!
这个分析挺不错的,这里贴不来一些
a.图片大小和尺寸太大出不来:图片是150X150 的,近40K,后来给制作重新做图,30X30的10k不到,分享图片大小最好不要超过32k ,尺寸100x100以上就行,别太大了,太小了图片失真
b.图片路径有{-}中划线:某些浏览器或手机对含有中划线的路径不【友好】,结果把图片放到上一层目录,避免类似.../year-imgs/head.png这种路径
c.图片后缀不行:图片做的是.png后缀格式的,网上资料说分享朋友圈的时候可能直接会忽略掉.png后缀的图片,最好使用.jpg的图片作为分享图片的格式
d.分享时调用接口没有取到图片地址:在页面的顶部(或者把分享的那张图片放在该页面所有图片的前面第一个位置)head里面放置一个图片(隐藏的)
这里问题总有一个问题是上面的,,个别除外
我的问题就是 图片尺寸大了,因为dug出来的图片大小再28000以上的大小是分享出来的图片不能显示,而再21000多点是可以分享出来的,这个数据需要出意1024大概再20多k以上,由此看来并没有超出32k ,也没有分享成功,那么有没有可能是图片尺寸太大的原因,这里图片分享的图片都铺满全屏的。。
先贴出部分分享成功的代码
/**
* Bitmap转换成byte[]并且进行压缩,压缩到不大于maxkb
* @param bitmap
* @param maxkb
* @return
*/
fun bmpToByteArray(bitmap: Bitmap, maxkb: Int): ByteArray? {
val output = ByteArrayOutputStream()
var options = 100
bm.compress(Bitmap.CompressFormat.PNG, options, output)
while (output.toByteArray().size/1024 > maxkb) {
output.reset() //清空output
bitmap.compress(Bitmap.CompressFormat.JPEG, options, output) //这里压缩options%,把压缩后的数据存放到output中
options -= 10
}
return output.toByteArray()
}
这端代码是有问题的,不是所有的链接缩略图会显示出来
贴上正确的
/**
* Bitmap转换成byte[]并且进行压缩,压缩到不大于maxkb
* @param bitmap
* @param maxkb
* @return
*/
fun bmpToByteArray(bitmap: Bitmap, maxkb: Int): ByteArray? {
val bm = Bitmap.createScaledBitmap(bitmap,100,100,true)
val output = ByteArrayOutputStream()
var options = 100
bm.compress(Bitmap.CompressFormat.PNG, options, output)
while (output.toByteArray().size/1024 > maxkb) {
output.reset() //清空output
bitmap.compress(Bitmap.CompressFormat.JPEG, options, output) //这里压缩options%,把压缩后的数据存放到output中
options -= 10
}
return output.toByteArray()
}
只是多了一段代码,就是按尺寸缩放大小,测试发现执行第一个的时候就已经是小于32kb了,后面那个压缩方法没有执行,这里采用的是两个压缩方法,先尺寸缩放后质量压缩,不知道会不会出现问题,现在暂时这里,后期遇到问题再做修改。但测试了几个是没有问题的。
把压缩的方法贴出来
质量压缩
/**
* 压缩图片
*
* @param bitmap
* 被压缩的图片
* @param sizeLimit
* 大小限制
* @return
* 压缩后的图片
*/
private Bitmap compressBitmap(Bitmap bitmap, long sizeLimit) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
// 循环判断压缩后图片是否超过限制大小
while(baos.toByteArray().length / 1024 > sizeLimit) {
// 清空baos
baos.reset();
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, baos);
quality -= 10;
}
Bitmap newBitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(baos.toByteArray()), null, null);
return newBitmap;
}
宽高压缩
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,int reqWidth, int reqHeight) {
final BitmapFactory.Options options = new BitmapFactory.Options();
// 先将inJustDecodeBounds设置为true不会申请内存去创建Bitmap,返回的是一个空的Bitmap,但是可以获取
//图片的一些属性,例如图片宽高,图片类型等等。
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// 计算inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// 加载压缩版图片
options.inJustDecodeBounds = false;
// 根据具体情况选择具体的解码方法
return BitmapFactory.decodeResource(res, resId, options);
}
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
// 原图片的宽高
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// 计算inSampleSize值
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}