以下程式碼將示範如何將Bitmap轉為指定格式的圖片並儲存至外部儲存裝置SDCard。


若要寫入SDCard,必須先將寫入外部儲存裝置的權限打開,於Android專案的AndroidMaifest.xml中加入以下敘述。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

try {
    // 取得外部儲存裝置路徑
    String path = Environment.getExternalStorageDirectory().toString ();
    // 開啟檔案
    File file = new File( path, "Image.png");
    // 開啟檔案串流
    FileOutputStrea out = new FileOutputStream(file );
    //  Bitmap壓縮成指定格式的圖片並寫入檔案串流
    bmp.compress ( Bitmap. CompressFormat.PNG , 90 , out);
    // 刷新並關閉檔案串流
    out.flush ();
    out.close ();
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace ();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace ();
}

其中bmp即為要轉為圖片儲存的Bitmap。
arrow
arrow
    全站熱搜

    Felix 發表在 痞客邦 留言(0) 人氣()