调用摄像头和相册(调用摄像头拍照和从相册中选择图片)

public class MainActivity extends Activity {
	public static final int TAKE_PHOTO=1;
	public static final int CROP_PHOTO=0;
	public static final int CHOOSE_PHOTO=3;
	private ImageView picture;
	private Button chooseFromAlbum;
	private Button takePhoto;
	private Uri imageUri;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		takePhoto=(Button)findViewById(R.id.take_photo);
		chooseFromAlbum=(Button)findViewById(R.id.choose_from_ablum);
		picture=(ImageView)findViewById(R.id.picture);
		takePhoto.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				File outputImage=new File(Environment.getExternalStorageDirectory(), "output_image.jpg");
				try {
					if(outputImage.exists()){
						outputImage.delete();
					}
					outputImage.createNewFile();
				} catch (Exception e) {
					e.printStackTrace();
				}
				imageUri=Uri.fromFile(outputImage);
				Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
				intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
				startActivityForResult(intent,TAKE_PHOTO);
				}
			
		});
		chooseFromAlbum.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent intent=new Intent("android.intent.action.GET_CONTENT");
				intent.setType("image/*");
				startActivityForResult(intent, CHOOSE_PHOTO);
				
			}
			
		});
	}
 
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case TAKE_PHOTO:
			if(resultCode==RESULT_OK){
				Intent intent=new Intent("com.android.camera.action.CROP");
				intent.setDataAndType(imageUri, "image/*");
				//intent.putExtra("scale", true);
				intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
				startActivityForResult(intent, CROP_PHOTO);
				
			}
			
			break;
		case CROP_PHOTO:
			if(resultCode==RESULT_OK){
				try {
					Bitmap bitmap=BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
					picture.setImageBitmap(bitmap);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		case CHOOSE_PHOTO:
			if(resultCode==RESULT_OK){
				//判断手机系统版本号
				if(Build.VERSION.SDK_INT>=19){//4.4及以上系统使用这个方法处理图片
					handleImageOnkitKat(data);
				}else{
					handleImageBeforekitKat(data);//4.4及以下系统使用这个方法处理图片
				}

			}
		default:
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	
	//API 19以下
	private void handleImageBeforekitKat(Intent data) {
				
		
	}
   //API 19
	private void handleImageOnkitKat(Intent data) {
		String imagePath=null;
		Uri uri=data.getData();
		if(DocumentsContract.isDocumentUri(MainActivity.this, uri)){
			String docId=DocumentsContract.getDocumentId(uri);
			if("com.android.providers.media.documents".equals(uri.getAuthority())){
				String id=docId.split(":")[1];
				String selection=MediaStore.Images.Media._ID+"="+id;
				imagePath=getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,selection);
				
			}else if("com.android.providers.downloads.documents".equals(uri.getAuthority())){
				Uri contentUri=ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),Long.valueOf(docId));
				imagePath=getImagePath(contentUri,null);
			}
		}else if("content".equalsIgnoreCase(uri.getScheme())){
			imagePath=getImagePath(uri, null);
		}
		displayImage(imagePath);
		
	}

	private void displayImage(String imagePath) {
		if(imagePath!=null){
			Bitmap bitmap=BitmapFactory.decodeFile(imagePath);
			picture.setImageBitmap(bitmap);
		}else{
			Toast.makeText(this, "failed to get image", Toast.LENGTH_SHORT).show();
		}
		
	}

	private String getImagePath(Uri uri, String selection) {
		String path=null;
		Cursor cursor=getContentResolver().query(uri, null, selection, null, null);
		if(cursor!=null){
			if(cursor.moveToFirst()){
				path=cursor.getString(cursor.getColumnIndex(Media.DATA));
			}
			cursor.close();
		}
		return path;
	}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值