保存Bitmap到本地文件夹

来源:互联网 发布:阿里云的产品好吗 编辑:程序博客网 时间:2024/05/16 05:12

[方法一]:


/** 保存方法 */
 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"/>


[方法二]:

  1. public void saveMyBitmap(String bitName, Bitmap mBitmap) throws IOException {  
  2.         File f = new File("/sdcard/" + bitName + ".png");  
  3.         f.createNewFile();  
  4.         FileOutputStream fOut = null;  
  5.         try {  
  6.                 fOut = new FileOutputStream(f);  
  7.         } catch (FileNotFoundException e) {  
  8.                 e.printStackTrace();  
  9.         }  
  10.         mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);  
  11.         try {  
  12.                 fOut.flush();  
  13.         } catch (IOException e) {  
  14.                 e.printStackTrace();  
  15.         }  
  16.         try {  
  17.                 fOut.close();  
  18.         } catch (IOException e) {  
  19.                 e.printStackTrace();  
  20.         }  
  21. }  

0 0
原创粉丝点击