微信分享圖片URL不顯示問題

很有可能是圖片太大,或者有敏感詞。
關於圖片太大的話,可以讓UI切個小的圖片。
或者自己用代碼壓縮。
先記錄一下:
在這裏插入圖片描述
首先用Glide下載下來圖片。
1、把Drawable轉換成Bitmap。

public static Bitmap drawableToBitmap(Drawable drawable) {

        // 獲取 drawable 長寬
        int width = drawable.getIntrinsicWidth();
        int heigh = drawable.getIntrinsicHeight();

        drawable.setBounds(0, 0, width, heigh);

        // 獲取drawable的顏色格式
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565;
        // 創建bitmap
        Bitmap bitmap = Bitmap.createBitmap(width, heigh, config);
        // 創建bitmap畫布
        Canvas canvas = new Canvas(bitmap);
        // 將drawable 內容畫到畫布中
        drawable.draw(canvas);
        return bitmap;
    }

2、把Bitmap轉換數組。

   public byte[] bmpToByteArray(final Bitmap bmp, final boolean needRecycle) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 32, output);
        if (needRecycle) {
            bmp.recycle();
        }

        byte[] result = output.toByteArray();
        try {
            output.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }

3、對Bitmap數組進行寬高尺寸壓縮。

private static Bitmap calculateInSampleSize( byte[] bitmapBytes,int reqWidth, int reqHeight) {
        Bitmap bitmapT=null;
        BitmapFactory.Options options = new BitmapFactory.Options();

        options.inJustDecodeBounds = true;
        BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length, options);
        int originalWidth = options.outWidth;
        int originalHeight = options.outHeight;

        int inSampleSize = 1;
        if (originalHeight > reqHeight || originalWidth > reqHeight){
            int halfHeight = originalHeight / 2;
            int halfWidth = originalWidth / 2;
            //壓縮後的尺寸與所需的尺寸進行比較
            while ((halfWidth / inSampleSize) >= reqHeight && (halfHeight /inSampleSize)>=reqWidth){
                inSampleSize *= 2;
            }

        }
        //獲取採樣率
        options.inSampleSize =inSampleSize;
        options.inJustDecodeBounds = false;

        bitmapT=BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length, options);
        return bitmapT;
    }

4、質量壓縮。

    public static byte[] qualityCompress1(Bitmap bitmap ,int reqSize){

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        //這裏100表示不壓縮,0 ~ 100 可以自行調整,把壓縮後的數據存放到baos中
        bitmap.compress(Bitmap.CompressFormat.JPEG,100,baos);
        int options = 95;
        //如果壓縮後的大小超出所要求的,繼續壓縮
        while (baos.toByteArray().length / 1024 > reqSize){
            baos.reset();
            bitmap.compress(Bitmap.CompressFormat.JPEG,options,baos);

            //每次減少5%質量
            if (options>5){//避免出現options<=0
                options -=5;
            } else {
                break;
            }
        }

        ByteArrayOutputStream bao = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, options, bao);

        return bao.toByteArray();
    }

5、最後得到壓縮後的Bitmap

bitmap =BitmapFactory.decodeByteArray(bytes2,0,bytes2.length);

菜鳥做此記錄。
。如果有大神有更好的方法歡迎。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章