ChoosePicTest
第一行代码的拍照和选择图库照片,有改动
演示
20160824_163601.gif笔记
点击拍照
-
创建file存储图片
<pre>
File outImage = new File(Environment.getExternalStorageDirectory(),"tempImg.png");
</pre> -
根据之前的路径,创建一个空文件
<pre>
outImage.createNewFile(); </pre> -
将file文件转换成Uri—— 形式:file://
<pre>
imageUri = Uri.fromFile(outImage);</pre> -
启动相机
<pre>
//启动相机程序
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
startActivityForResult(intent,TAKE_PHOTO);
</pre>
或者 -
打开相册
<pre>
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*"); intent.putExtra("crop",true);
intent.putExtra("scale",true);
intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
startActivityForResult(intent,SELECT_PHOTO);
</pre>
选择图库相片
1,2,3步骤同上
- 设置intent
<pre>
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
intent.putExtra("crop",true);
intent.putExtra("scale",true); intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri); startActivityForResult(intent,SELECT_PHOTO);
</pre>
onActivityResult处理——注意两处获取bitmap的方式是不一样的
拍照和裁剪
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 {
// 把InputStream转换成bitmap
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
//将裁减后的照片显示出来
mImageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
break;
选择并显示
case SELECT_PHOTO:
ContentResolver resolver = getContentResolver();
//照片的原始资源地址
Uri uri = data.getData();
Log.d(TAG, "onActivityResult: "+uri);
try {
Bitmap photo = MediaStore.Images.Media.getBitmap(resolver,uri);
if (photo != null){
mImageView.setImageBitmap(photo);
}
}catch (IOException e){
e.printStackTrace();
}
网友评论