android保存bitmap

来源:互联网 发布:网络党建工作调研报告 编辑:程序博客网 时间:2024/05/18 00:07

 // 保存到sdcard
    public  void savePic(Bitmap b) {

        FileOutputStream fos = null;
        try {
        Log.i(TAG,"start savePic");
       
//         String sdpath ="/storage/sdcard1/";
        File f = new File(sdpath ,"11.bmp");
        if (f.exists()) {
        f.delete();
        }
       
            fos = new FileOutputStream(f);
            Log.i(TAG,"strFileName 1= " + f.getPath());
            if (null != fos) {
                b.compress(Bitmap.CompressFormat.PNG, 90, fos);
                fos.flush();
                fos.close();
                Log.i(TAG,"save pic OK!");
            }
        } catch (FileNotFoundException e) {
        Log.i(TAG,"FileNotFoundException");
            e.printStackTrace();
        } catch (IOException e) {
        Log.i(TAG,"IOException");
            e.printStackTrace();
        }
    }
    

在这里还需要两个权限:

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

0 0