android中Bitmap对象怎么保存为文件?

rt

  • snowtank - 1年前

    Bitmap类有一compress成员,可以把bitmap保存到一个stream中。
    例如:

    public void saveMyBitmap(String bitName) throws IOException {
                            File f = new File("/sdcard/Note/" + bitName + ".png");
                            f.createNewFile();
                            FileOutputStream fOut = null;
                            try {
                                    fOut = new FileOutputStream(f);
                            } catch (FileNotFoundException e) {
                                    e.printStackTrace();
                            }
                            mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                            try {
                                    fOut.flush();
                            } catch (IOException e) {
                                    e.printStackTrace();
                            }
                            try {
                                    fOut.close();
                            } catch (IOException e) {
                                    e.printStackTrace();
                            }
                    }