儲存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的協助文檔。