Android拍照,选择照片

启动相机拍照

//用相机拍照获取图片
private void takePhoto() {  
  imgUri = HoYoImgHelper.getOutPutPicFileUri();  
  Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);   
  takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);  
  startActivityForResult(takePhotoIntent, TakePhotoRequestCode);
}

在onActivityResult()中处理返回结果

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);   
  if (resultCode == RESULT_OK) {     
      switch (requestCode) {
           case TakePhotoRequestCode:   
                try {       
                    showSelImage(imgUri.getPath());
                } catch (Exception ex) {  
                    ex.printStackTrace();   
                }
            break;
      }
   }
}

imgUri获取方法:

//获取图片要保存的uri
public static Uri getOutPutPicFileUri() {
    return Uri.fromFile(getOutPutPicFile());
}

//获取图片要保存的路径
public static File getOutPutPicFile() {
    File mediaStorageDir = null;  
    try {        
        mediaStorageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM); 
    } catch (Exception e) {
        e.printStackTrace(); 
    }   
    // Create the storage directory if it does not exist    
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            return null;      
        }    
    }    
   // Create a media file name 
   String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
   File mediaFile;    
   mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg"); 
   return mediaFile;
}

选择图片

Intent selPicIntent = new Intent();
selPicIntent.setType("image/*");
selPicIntent.setAction(Intent.ACTION_PICK);
startActivityForResult(selPicIntent, SelPicRequestCode);

或者

Intent selPicIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
selPicIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
startActivityForResult(selPicIntent, SelPicRequestCode);

在onActivityResult()中处理返回结果

case SelPicRequestCode:    
try {      
    Uri selectedImage = data.getData();    
    String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
    Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);  
    cursor.moveToFirst(); 
    ContentResolver cr = this.getContentResolver();
    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
    String picturePath = cursor.getString(columnIndex);
    showSelImage(picturePath);
}catch (Exception ex) {
    ex.printStackTrace();
}  
break;

其中ShowSelImage(String imgPath)方法是根据图片绝对路径显示到指定控件上的方法

注:选择图片版本相关

    Intent selPicIntent = new Intent();
    selPicIntent.setType("image/*");
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
        selPicIntent.setAction(Intent.ACTION_PICK);
    } else {
        selPicIntent.setAction(Intent.ACTION_GET_CONTENT);
    }
    startActivityForResult(selPicIntent, SelPicRequestCode);

onActivityResult()中处理方法

String picturePath;
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1) {
      String[] filePathColumn = {MediaStore.Images.Media.DATA};
      Cursor cursor = getContentResolver().query(selectedImage,
                  filePathColumn, null, null, null);
      cursor.moveToFirst();
      int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
      picturePath = cursor.getString(columnIndex);
} else {
      picturePath = selectedImage.getPath();
}

你可能感兴趣的:(Android拍照,选择照片)