调用系统的相机 保存图片指定路径,供图库查看
来源:互联网 发布:马云贵州大数据怎么了 编辑:程序博客网 时间: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
- 调用系统的相机 保存图片指定路径,供图库查看
- android 调用系统图库查看指定路径的图片
- android 调用系统图库打开指定路径的图片
- 调用系统相机拍照片保存到指定的路径
- Android调用系统的相机,图库,以及裁剪图片功能。
- 调用系统相机和图库册得到图片的例子
- ios 调用系统相机,图库,相册。将照片保存在相册,保存图片在本地,UIImagePickerController
- 调用系统相机和图库,裁剪图片
- 调用系统相机、图库并选择图片
- Android调用相机拍照并返回路径和调用系统图库选择图片
- Android调用相机拍照并返回路径和调用系统图库选择图片
- Android 调用系统相机、图库 ,取图片并裁剪、保存sd卡
- android调用系统相机,系统图库,系统剪裁图片
- 调用系统相机,截图工具与图库(调用系统的图片内容提供者)
- Android 调用系统相机和图库,裁剪图片
- Android调用系统图库和相机获取图片并裁剪
- 调用系统相机与图库
- 调用系统相机和图库
- 【详解】将查出来的元组数据作参数带入
- notepad++ 添加到右键菜单
- 郑大第九届校赛正式赛(1818: squee_spoon and his Cube VI)
- cmake 学习笔记(三)
- jQuery之事件绑定&合成事件
- 调用系统的相机 保存图片指定路径,供图库查看
- 工作中git的常见用法
- 【翻译】Android安全之Linux内核篇
- FZU - 2188过河I【BFS】
- 电信,移动,联通连接计费(android平台)
- div包含img时height增大的问题
- PAT1011
- 推荐系统的架构
- Bug1---Debug文件中的exe不能运行