android儲存bitmap到指定路徑,androidbitmap

來源:互聯網
上載者:User

android儲存bitmap到指定路徑,androidbitmap

不難,但用的時候有時候突然會想不起來。。記錄一下吧

先加許可權

1 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

 

 1 private void saveCroppedImage(Bitmap bmp) { 2         File file = new File("/sdcard/myFolder"); 3         if (!file.exists()) 4             file.mkdir(); 5  6         file = new File("/sdcard/temp.jpg".trim()); 7         String fileName = file.getName(); 8         String mName = fileName.substring(0, fileName.lastIndexOf(".")); 9         String sName = fileName.substring(fileName.lastIndexOf("."));10 11         // /sdcard/myFolder/temp_cropped.jpg12         String newFilePath = "/sdcard/myFolder" + "/" + mName + "_cropped" + sName;13         file = new File(newFilePath);14         try {15             file.createNewFile();16             FileOutputStream fos = new FileOutputStream(file);17             bmp.compress(CompressFormat.JPEG, 50, fos);18             fos.flush();19             fos.close();20         } catch (IOException e) {21             // TODO Auto-generated catch block22             e.printStackTrace();23         }24 25     }

 這裡比較重要的方法就是

bmp.compress(CompressFormat.JPEG, 50, fos);

文檔如下:

boolean android.graphics.Bitmap.compress(CompressFormat format, int quality, OutputStream stream)

 

Write a compressed version of the bitmap to the specified outputstream. If this returns true, the bitmap can be reconstructed by passing a corresponding inputstream to BitmapFactory.decodeStream(). Note: not all Formats support all bitmap configs directly, so it is possible that the returned bitmap from BitmapFactory could be in a different bitdepth, and/or may have lost per-pixel alpha (e.g. JPEG only supports opaque pixels).

Parameters:

format The format of the compressed image

quality Hint to the compressor, 0-100. 0 meaning compress for small size, 100 meaning compress for max quality. Some formats, like PNG which is lossless, will ignore the quality setting

stream The outputstream to write the compressed data.

Returns:

true if successfully compressed to the specified stream.

 

第一個參數有三種,源碼如下

 1 /** 2      * Specifies the known formats a bitmap can be compressed into 3      */ 4     public enum CompressFormat { 5         JPEG    (0), 6         PNG     (1), 7         WEBP    (2); 8  9         CompressFormat(int nativeInt) {10             this.nativeInt = nativeInt;11         }12         final int nativeInt;13     }

因為轉換的原始bitmap尾碼有不同,比如我用的jpg檔案,儲存下來也是jpg檔案。但用的是CompressFormat.JPEG做參數。

這個參數應該和原始檔案類型有關,這是個知識點,日後研究研究,寫個根據類型自動選擇參數的方法。

附上幾個連結:

android bitmap compress(圖片壓縮)

Android中文API(136) —— Bitmap

 


android開發 通過URL返迴文件並儲存到指定的SD卡路徑中

public static void writeFileData(String fileName, String message){
try{
FileOutputStream output = new FileOutputStream("/sdcard/" + fileName);
byte[] bytes = message.getBytes();
output.write(bytes);
output.close();
}catch(Exception e){
e.printStackTrace();
}
}
自己指定檔案名稱,寫檔案就可以了。
 
android中Bitmap存為一張圖片

可以用Bitmap.compress函數來把Bitmap對象儲存成PNG或JPG檔案,然後可以用BitmapFactory把檔案中的資料讀進來再產生Bitmap對象。
儲存的代碼大概類似於這樣:
try {
FileOutputStream out = new FileOutputStream(filename);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (Exception e) {
e.printStackTrace();
}
具體的可以去查Bitmap和BitmapFactory的協助文檔。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.