android中的BitMap:向SD卡中存如一个BitMap

允我心安 提交于 2019-12-06 12:58:46

 1.参考

//得到外部存储卡的路径

String path=Environment.getExternalStorageDirectory().toString();

//ff.png是将要存储的图片的名称
  File file=new File(path, "ff.png");

//从资源文件中选择一张图片作为将要写入的源文件
  Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ss);
  try {
   FileOutputStream out=new FileOutputStream(file);
   bitmap.compress(CompressFormat.PNG, 100, out);
   out.flush();
   out.close();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

一定要不要忘记加权限

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

2.应用

public void drawableTopicture(Drawable drawable) {  
            /*
             * Drawable转化为Bitmap 
            */
            int width = drawable.getIntrinsicWidth();  
            int height = drawable.getIntrinsicHeight();  
            Bitmap bitmap = Bitmap.createBitmap(width, height,  
                                          drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888: Bitmap.Config.RGB_565);  
            Canvas canvas = new Canvas(bitmap);  
            drawable.setBounds(0,0,width,height);  
            drawable.draw(canvas); 
            String filepath = null;
            
			/*创建目录*/
           if(Tools.hasSdcard()){
            	filepath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/HeadImage";
            	File f = new File(filepath);
            	if(!f.exists()){
            		f.mkdir();
            	}
            }
           headImgae_filename = new File(filepath,"headimage.png");
            /*将bitmap保存为图片文件*/
            FileOutputStream fos = null;
			try {
				fos = new FileOutputStream(headImgae_filename);
			
	            bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
					fos.flush();
					fos.close();
				} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

        }



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!