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

 1.參考java

//獲得外部存儲卡的路徑android

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

//ff.png是將要存儲的圖片的名稱
  File file=new File(path, "ff.png");code

//從資源文件中選擇一張圖片做爲將要寫入的源文件
  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();
  }orm

必定要不要忘記加權限圖片

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

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();
			}

        }
相關文章
相關標籤/搜索