调用系统的相机 保存图片指定路径,供图库查看

来源:互联网 发布:马云贵州大数据怎么了 编辑:程序博客网 时间:2024/04/28 08:03
static final int REQUEST_IMAGE_CAPTURE = 1;private void dispatchTakePictureIntent() {    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);    }}
注意在调用startActivityForResult()方法之前,先调用resolveActivity(),这个方法会返回能处理该Intent的第一个Activity(译注:即检查有没有能处理这个Intent的Activity)。执行这个检查非常重要,因为如果在调用startActivityForResult()时,没有应用能处理你的Intent,应用将会崩溃。所以只要返回结果不为null,使用该Intent就是安全的。

Android的相机应用会把拍好的照片编码为缩小的Bitmap,使用extra value的方式添加到返回的Intent当中,并传送给onActivityResult(),对应的Key为"data"。下面的代码展示的是如何获取这一图片并显示在ImageView上。

@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {        Bundle extras = data.getExtras();        Bitmap imageBitmap = (Bitmap) extras.get("data");        mImageView.setImageBitmap(imageBitmap);    }}
把照片保存到指定的目录中
创建一个路径
private File createImageFile() throws IOException {
    // Create an image file name    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());    String imageFileName = "JPEG_" + timeStamp + "_";    File storageDir = Environment.getExternalStoragePublicDirectory(            Environment.DIRECTORY_PICTURES);    File image = File.createTempFile(        imageFileName,  /* prefix */        ".jpg",         /* suffix */        storageDir      /* directory */    );    // Save a file: path for use with ACTION_VIEW intents    mCurrentPhotoPath = "file:" + image.getAbsolutePath();    return image;}
把照片保存到指定的目录下
static final int REQUEST_TAKE_PHOTO = 1;private void dispatchTakePictureIntent() {    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);    // Ensure that there's a camera activity to handle the intent    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {        // Create the File where the photo should go        File photoFile = null;        try {            photoFile = createImageFile();        } catch (IOException ex) {            // Error occurred while creating the File            ...        }        // Continue only if the File was successfully created        if (photoFile != null) {            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,                    Uri.fromFile(photoFile));            startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);        }    }}
将照片添加到相册中,以便系统的图片浏览可以观看
private void galleryAddPic() {    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);    File f = new File(mCurrentPhotoPath);    Uri contentUri = Uri.fromFile(f);    mediaScanIntent.setData(contentUri);    this.sendBroadcast(mediaScanIntent);}

缩放图片进行展示
private void setPic() {    // Get the dimensions of the View    int targetW = mImageView.getWidth();    int targetH = mImageView.getHeight();    // Get the dimensions of the bitmap    BitmapFactory.Options bmOptions = new BitmapFactory.Options();    bmOptions.inJustDecodeBounds = true;    BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);    int photoW = bmOptions.outWidth;    int photoH = bmOptions.outHeight;    // Determine how much to scale down the image    int scaleFactor = Math.min(photoW/targetW, photoH/targetH);    // Decode the image file into a Bitmap sized to fill the View    bmOptions.inJustDecodeBounds = false;    bmOptions.inSampleSize = scaleFactor;    bmOptions.inPurgeable = true;    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);    mImageView.setImageBitmap(bitmap);}

1 0
原创粉丝点击