最近再分享視訊的時候出現一個問題縮略圖不顯示有些顯示,雖然以前也遇到過但是忘了。今天這裡再寫一下友善記錄
百度了一下很多!!!
微信分享功能,安卓手機分享圖檔不顯示
android微信分享縮略圖不顯示
這個分析挺不錯的,這裡貼不來一些
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了,後面那個壓縮方法沒有執行,這裡采用的是兩個壓縮方法,先尺寸縮放後品質壓縮,不知道會不會出現問題,現在暫時這裡,後期遇到問題再做修改。但測試了幾個是沒有問題的。
把壓縮的方法貼出來
Android bitmap壓縮方法
品質壓縮
/**
* 壓縮圖檔
*
* @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;
}