Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 將 Bitmap 保存為 PNG 圖片的簡潔程式碼

編輯:關於 Android 程式設計

下面這段簡潔的程式碼,示範了如何把 Bitmap 物件保存為 PNG 圖片檔案,具體如下:

public static void saveBitmapAsPng(Bitmap bmp, File f) {
    try {
        FileOutputStream out = new FileOutputStream(f);
        bmp.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.flush();
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

這裡的核心在於 Bitmap 的 compress 方法:第一個參數指定輸出格式為 PNG,第二個參數是壓縮品質(0–100,PNG 為無損格式,傳 100 即可),第三個參數則是輸出的串流。

總結

以上程式碼非常簡單,把 Bitmap 壓縮寫入 FileOutputStream 即可,希望對你有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved