Android学习(15)-摄像头和相册
来源:互联网 发布:逆战数据异常规范游戏 编辑:程序博客网 时间:2024/06/12 21:19
如何调用自己手机的拍照功能以及从手机相册选择图片? 比如找个小例子:
首先,还是弄一个layout先:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ImageView android:id="@+id/picture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/take_photo" android:layout_alignRight="@+id/choose_from_album" android:layout_below="@+id/choose_from_album" android:layout_marginTop="55dp" android:src="@drawable/ic_launcher" /> <Button android:id="@+id/choose_from_album" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/take_photo" android:layout_alignBottom="@+id/take_photo" android:layout_alignParentRight="true" android:layout_marginRight="28dp" android:text="从相册选择图片" /> <Button android:id="@+id/take_photo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="14dp" android:layout_marginTop="23dp" android:text="拍照" /></RelativeLayout>
对于该活动,其代码:
public class MainActivity extends Activity {public static final int TAKE_PHOTO = 1;public static final int CROP_PHOTO = 2; //裁剪图片//用于拍照private Button takePhoto;private ImageView picture;private Uri imageUri; //选择图片private Button chooseFromAlbum;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);takePhoto = (Button)findViewById(R.id.take_photo);picture = (ImageView)findViewById(R.id.picture);takePhoto.setOnClickListener(new OnClickListener() {public void onClick(View arg0) {//创建File对象,用于存储拍照后的图片File outputImage = new File(Environment.getExternalStorageDirectory(),"tempImage.jpg");//参数: 目录 文件try {if(outputImage.exists()){ //文件已经存在就删除outputImage.delete();}outputImage.createNewFile();} catch (IOException 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);//启动拍照服务,而且需要返回结果,返回数据由onActivityResult处理}});chooseFromAlbum = (Button)findViewById(R.id.choose_from_album);chooseFromAlbum.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {//创建File文件,用于存储选择的照片File outputImage = new File(Environment.getExternalStorageDirectory(),"output_image.jpg");try {if(outputImage.exists()){outputImage.delete();}outputImage.createNewFile();} catch (IOException e) {e.printStackTrace();}imageUri = Uri.fromFile(outputImage); //打开相册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, CROP_PHOTO);}});}//用于响应调用活动返回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 (FileNotFoundException e) {e.printStackTrace();}}break;default:break;}}}
在配置文件中要配置权限说明:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
0 0
- Android学习(15)-摄像头和相册
- Android学习之调用摄像头和相册
- Android学习-如何调用摄像头和相册
- Android调用摄像头和相册
- Android调用摄像头和相册
- Android 使用摄像头和相册
- Android调用摄像头和相册
- android 学习笔记 调用系统摄像头和相册
- 【Android】安卓学习笔记之调用摄像头和相册
- android学习十五(android系统中调用摄像头和相册)
- Android调用摄像头和本地相册
- 17 | 摄像头和相册
- 调用摄像头和相册
- 调用摄像头和相册
- 调用摄像头和相册(调用摄像头拍照和从相册中选择图片)
- Android学习笔记(八)使用多媒体(通知、短信、摄像头、相册、音视频)
- Android调用摄像头拍照和从相册中选择(上传、更换头像)
- Android之调用摄像头和相册以及裁剪
- spring batch学习笔记之元数据表
- c++学习 定位new表达式
- 14个最受欢迎的Python开源框架
- wifi direct (wifi peer to peer)
- SQL Server 触发器的管理与查看
- Android学习(15)-摄像头和相册
- oracle编程入门笔记2015-01-18--常用oracle查询语句
- 苹果开发者账号申请与iTunesconnect中心问题联系电话
- 学习Python编程的11个资源
- [Swift]Swift的函数式编程
- SPI总线协议介绍
- ubuntu开启SSH服务
- GPS NMEA-0183协议详解
- PCB抄板流程