Android拍照并获取图片

一:拍照后在Activity的函数中处理bitmap,此时图片需要自己保存到指定路径

protected void getImageFromCamera() {  
       String state = Environment.getExternalStorageState();  
       if (state.equals(Environment.MEDIA_MOUNTED)) {  
           Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");     
           startActivityForResult(getImageByCamera, REQUEST_CODE_CAPTURE_CAMEIA);  
       }  
       else {  
           Toast.makeText(getApplicationContext(), "请确认已经插入SD卡", Toast.LENGTH_LONG).show();  
       }  
   } 

@Override  
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
       if (requestCode == REQUEST_CODE_CAPTURE_CAMEIA ) {             
       Uri uri = data.getData();  
       if(uri == null){  
           //use bundle to get data  
           Bundle bundle = data.getExtras();    
               if (bundle != null) {                 
               Bitmap  photo = (Bitmap) bundle.get("data"); //get bitmap  
               //spath :生成图片取个名字和路径包含类型                              
               saveImage(Bitmap photo, String spath);  
               } else {           
                   Toast.makeText(getApplicationContext(), "err****", Toast.LENGTH_LONG).show();           
                return;        
                }    
       }else{  
                //to do find the path of pic by uri  
       }   
   }  
}  
public static void saveImage(Bitmap photo, String spath) {  
        try {  
            BufferedOutputStream bos = new BufferedOutputStream(  
                    new FileOutputStream(spath, false));  
            photo.compress(Bitmap.CompressFormat.JPEG, 100, bos);  
            bos.flush();  
            bos.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
            return false;  
        }  
        return true;  
    }  

二:在拍照时直接指定图片保存路径

/**
	 * 通过拍照获取图片
	 */
	protected void getImageFromCamera() {
		String state = Environment.getExternalStorageState();
		if (state.equals(Environment.MEDIA_MOUNTED)) {
			Intent getImageByCamera = new Intent("android.media.action.IMAGE_CAPTURE");
			String out_file_path = SAVED_IMAGE_DIR_PATH;
			File dir = new File(out_file_path);
			if (!dir.exists()) {
				dir.mkdirs();
			}
			String capturePath = SAVED_IMAGE_DIR_PATH + System.currentTimeMillis() + ".jpg";
			getImageByCamera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(capturePath)));
			getImageByCamera.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
			startActivityForResult(getImageByCamera, REQUEST_CODE_CAPTURE_CAMEIA);
		} else {
			Toast.makeText(getApplicationContext(), "请确认已经插入SD卡", Toast.LENGTH_LONG).show();
		}
	}

 

 

0 条评论
发表一条评论