您的位置:首页 > 其它

保存Bitmap到本地文件夹

2016-06-03 12:11 169 查看
[方法一]:

/** 保存方法 */

 public void saveBitmap() {

  Log.e(TAG, "保存图片");

  File f = new File("/sdcard/namecard/", picName);

  if (f.exists()) {

   f.delete();

  }

  try {

   FileOutputStream out = new FileOutputStream(f);

   bm.compress(Bitmap.CompressFormat.PNG, 90, out);

   out.flush();

   out.close();

   Log.i(TAG, "已经保存");

  } catch (FileNotFoundException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

 }

 

在这里还需要两个权限:

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

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

[方法二]:

public void saveMyBitmap(String bitName, Bitmap mBitmap) throws IOException {  

        File f = new File("/sdcard/" + 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();  

        }  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: