日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

Bitmap之compress图片压缩

發(fā)布時間:2023/12/15 综合教程 24 生活家
生活随笔 收集整理的這篇文章主要介紹了 Bitmap之compress图片压缩 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
package com.loaderman.customviewdemo;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.widget.ImageView;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView iv_1 = (ImageView) findViewById(R.id.img1);
        ImageView iv_2 = (ImageView) findViewById(R.id.img2);

        Bitmap bmp = BitmapFactory.decodeResource(this.getResources(), R.drawable.cat);
        iv_1.setImageBitmap(bmp);

        //壓縮圖像后,顯示
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 1, bos);
        byte[] bytes = bos.toByteArray();
        Bitmap bmp1 = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        iv_2.setImageBitmap(bmp1);


        ImageView iv_3 = (ImageView) findViewById(R.id.img3);
        ImageView iv_4 = (ImageView) findViewById(R.id.img4);


        ByteArrayOutputStream bos3 = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 1, bos3);

        byte[] bytes3 = bos3.toByteArray();
        Bitmap bmp3 = BitmapFactory.decodeByteArray(bytes3, 0, bytes3.length);

        iv_3.setImageBitmap(bmp3);


        ByteArrayOutputStream bos4 = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.WEBP, 1, bos4);

        byte[] bytes4 = bos4.toByteArray();
        Bitmap bmp4 = BitmapFactory.decodeByteArray(bytes4, 0, bytes4.length);

        iv_4.setImageBitmap(bmp4);

    }


    /**
     * 保存文件到手機(jī)SD卡根目錄中
     * @param bitmap
     */
    private void saveBmp(Bitmap bitmap) {
        File fileDir = Environment.getExternalStorageDirectory();
        String path = fileDir.getAbsolutePath() + "/lavor.webp";

        File file = new File(path);
        if (file.exists()) {
            file.delete();
        }
        try {
            FileOutputStream outputStream = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.WEBP, 10, outputStream);
            outputStream.flush();
            outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

效果:

總結(jié)

以上是生活随笔為你收集整理的Bitmap之compress图片压缩的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。