儲存Bitmap到SD卡,儲存BitmapSD卡

來源:互聯網
上載者:User

儲存Bitmap到SD卡,儲存BitmapSD卡

public static void saveBitmapInExternalStorage(Bitmap bitmap,Context context) {try {if(IsExternalStorageAvailableAndWriteable()) {File extStorage = new File(Environment.getExternalStorageDirectory().getPath() +"/orimuse");//orimuse為SD卡下一個檔案夾if (!extStorage.exists()) {extStorage.mkdirs();}File file = new File(extStorage,System.currentTimeMillis()+".png");FileOutputStream fOut = new FileOutputStream(file);bitmap.compress(Bitmap.CompressFormat.PNG, 90, fOut);//壓縮圖片fOut.flush();fOut.close();Toast.makeText(context,  "儲存成功", Toast.LENGTH_SHORT).show();}else {Toast.makeText(context, "儲存失敗", Toast.LENGTH_SHORT).show();}}catch (IOException ioe){ioe.printStackTrace();}}


android怎由數組儲存成圖片並儲存在SD卡上

Bitmap bm = BitmapFactory.decodeByteArray(byte[] data, int offset, int length);別忘了判斷數組是不是為空白。

儲存。。。。
public void saveFile(Bitmap bm, String fileName) throws IOException {
private final static String ALBUM_PATH
= Environment.getExternalStorageDirectory() + "/download_test/";

File dirFile = new File(ALBUM_PATH);
if(!dirFile.exists()){
dirFile.mkdir();
}
File myCaptureFile = new File(ALBUM_PATH + fileName);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
bos.flush();
bos.close();
}
 
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.