Android 调用系统相机

1、按钮触发调用系统相机还是相册,以及是否裁剪

//=================选择照片======start=====================================================================
	
		private String imagePath;
		private boolean crop = true;
		private boolean isSave = false;
		private  final int m_width = 400;
		private  final int m_height = 400;


public void takePhoto() {
	// TODO Auto-generated method stub
	imagePath = getTakePhotoPath();
	Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//"android.media.action.IMAGE_CAPTURE"
	intent.putExtra(MediaStore.EXTRA_OUTPUT,
			Uri.fromFile(new File(imagePath)));
	if(crop)
	{
		startActivityForResult(intent,
				RequestCode.TakePhoto_crop.getCode());
	}else
	{
		startActivityForResult(intent,
				RequestCode.TakePhoto.getCode());
	}
	

}


public void pickPhoto() {
	// TODO Auto-generated method stub

	Intent intent1 = new Intent(Intent.ACTION_PICK);
	intent1.setType("image/*");
	intent1.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
	
	if(crop)
	{
		startActivityForResult(intent1,
				RequestCode.PickPhoto_crop.getCode());
	}else
	{
		startActivityForResult(intent1,
				RequestCode.PickPhoto.getCode());
	}
	
	
}


	/***
	 * 配置拍照后图片在本地的地址
	 * @return
	 */
	public String getTakePhotoPath() {
		
		String SDCARD = Environment.getExternalStorageDirectory()
				.getAbsolutePath();
		File file = new File(SDCARD + "/yashu/pic/");
		if (!file.exists()) {
			file.mkdirs();
		}
		
		
		String imagePath = SDCARD + "/yashu/pic/"
				+ System.currentTimeMillis() + ".jpg";

		return imagePath;
	}
	


public enum RequestCode {

	OK {
		
		@Override
		public int getCode() {
			// TODO Auto-generated method stub
			return 0;
		}
		
	},
	PickPhoto {

		@Override
		public int getCode() {
			// TODO Auto-generated method stub
			return 105;
		}

	},
	TakePhoto {
		
		@Override
		public int getCode() {
			// TODO Auto-generated method stub
			return 106;
		}
		
	},
	PickPhoto_crop {

		@Override
		public int getCode() {
			// TODO Auto-generated method stub
			return 107;
		}

	},
	TakePhoto_crop {

		@Override
		public int getCode() {
			// TODO Auto-generated method stub
			return 108;
		}

	},
	IntentData {
		
		@Override
		public int getCode() {
			// TODO Auto-generated method stub
			return 98;
		}
		
	};

	public abstract int getCode();
}





2、回调函数,如果图片需要保存到相册,isSave = true;


		private Uri uuUri;
		
		@Override
		protected void onActivityResult(int requestCode, int resultCode,
				final Intent data) {
			// TODO Auto-generated method stub
			super.onActivityResult(requestCode, resultCode, data);

			
			if (requestCode == RequestCode.PickPhoto.getCode()
					&& resultCode == Activity.RESULT_OK) {

				Uri uri = data.getData();
				Cursor cursor = getContentResolver().query(uri, null, null, null,
						null);
				cursor.moveToFirst();
				String imagePath = cursor.getString(1); // 图片文件路径
				
				
			}
			else if (requestCode == RequestCode.TakePhoto.getCode()
					&& resultCode == Activity.RESULT_OK) {

			
			}
			else if (requestCode == RequestCode.TakePhoto_crop.getCode()
					&& resultCode == Activity.RESULT_OK) {
				
				File file = new File(imagePath);
				if(isSave && file.exists())
				{
				/*	Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
					Uri uri = Uri.fromFile(file);
					intent.setData(uri);
					mContext.sendBroadcast(intent);*/
					
					
					 try {
						String picPath = MediaStore.Images.Media.insertImage(mContext.getContentResolver(),
									file.getAbsolutePath(), file.getName(), null);
						

						//sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, uuUri));  
						// mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uuUri)); 
						 
					//	mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); 
						
						uuUri= Uri.parse(picPath);
					//mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uuUri)); 
						
						 String[] proj = { MediaStore.Images.Media.DATA };
		                    Cursor actualimagecursor = this.managedQuery(Uri.parse(picPath),proj,null,null,null);
		                    int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		                    actualimagecursor.moveToFirst();
		                    String img_path = actualimagecursor.getString(actual_image_column_index);
		                    //Log.d("StorageActivity", "married is " + img_path);

		                    MediaScannerConnection.scanFile(this, new String[]{ img_path}, null, null);
		                    
						//sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
						
						//mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse(file.getAbsolutePath())));
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						//e.printStackTrace();
					}
				}
				
				
				Uri uri = Uri.fromFile(new File(imagePath));
				
				imagePath =  ConfigManager.getInstance().getTakePhotoPath();
				cropImage(uri, m_width, m_width, RequestCode.TakePhoto.getCode(),imagePath);
			}
			else if (requestCode == RequestCode.PickPhoto_crop.getCode()
					&& resultCode == Activity.RESULT_OK) {
				
				Uri uri = data.getData();
				imagePath =  ConfigManager.getInstance().getTakePhotoPath();
				cropImage(uri, m_width, m_width, RequestCode.TakePhoto.getCode(),imagePath);
			}
			
		}

		//截取图片
		public void cropImage(Uri uri, int outputX, int outputY, int requestCode,String path){
			Intent intent = new Intent("com.android.camera.action.CROP");  
	        intent.setDataAndType(uri, "image/*");  //path);//
	        intent.putExtra("crop", "true");  
	
	        if(android.os.Build.MANUFACTURER.contains("HUAWEI"))
	        {//华为特殊处理 不然会显示圆
	        	intent.putExtra("aspectX", 9998);  
	            intent.putExtra("aspectY", 9999);
	        }
	        else
	        {
	        	intent.putExtra("aspectX", 1);  
	            intent.putExtra("aspectY", 1);
	        }
	        
	        
	        intent.putExtra("outputX", outputX);   
	        intent.putExtra("outputY", outputY); 
	        intent.putExtra("outputFormat", "JPEG");
	        intent.putExtra("noFaceDetection", true);
	        intent.putExtra("return-data", false);  
	        //intent.putExtra("circleCrop", false);
	        
	        intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(path))); 
	        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); 
	   
	        
		
		    startActivityForResult(intent, requestCode);
		}
		




猜你喜欢

转载自blog.csdn.net/wanglei19880622/article/details/52093558