Android开发之 选择相册图片或者拍照
2016-06-06 15:47
423 查看
在我们的Android开发中肯定都会遇到要选择本地相册的图片或者是用相机进行拍照使用图片。
简单的讲解一下如何使用,比如我们要上传头像时总会使用的。这个步骤一般就是:1.点击头像然后会弹出一个菜单,然后我们选择是从相册中选择图片还是使用相机拍照;2.选择照片或者拍照以后保存后照片的Id。基本就是这个样子比较简单的功能
布局文件:
简单的讲解一下如何使用,比如我们要上传头像时总会使用的。这个步骤一般就是:1.点击头像然后会弹出一个菜单,然后我们选择是从相册中选择图片还是使用相机拍照;2.选择照片或者拍照以后保存后照片的Id。基本就是这个样子比较简单的功能
public class MainActivity extends Activity { private static final int PHOTO_REQUEST_CAMERA = 1;// 拍照 private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择 private static final int PHOTO_REQUEST_CUT = 3;// 结果 private ImageView mFace; private Bitmap bitmap; /* 头像名称 */ private static final String PHOTO_FILE_NAME = "temp_photo.jpg"; private File tempFile; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.mFace = (ImageView) this.findViewById(R.id.iv_image); } /* * 从相册获取 */ public void gallery(View view) { // 激活系统图库,选择一张图片 Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("image/*"); this.startActivityForResult(intent, PHOTO_REQUEST_GALLERY); } /* * 从相机获取 */ public void camera(View view) { Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); // 判断存储卡是否可以用,可用进行存储 if (hasSdcard()) { intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment .getExternalStorageDirectory(), PHOTO_FILE_NAME))); } this.startActivityForResult(intent, PHOTO_REQUEST_CAMERA); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PHOTO_REQUEST_GALLERY) { if (data != null) { // 得到图片的全路径 Uri uri = data.getData(); crop(uri); } } else if (requestCode == PHOTO_REQUEST_CAMERA) { if (hasSdcard()) { tempFile = new File(Environment.getExternalStorageDirectory(), PHOTO_FILE_NAME); crop(Uri.fromFile(tempFile)); } else { Toast.makeText(MainActivity.this, "未找到存储卡,无法存储照片!", 0).show(); } } else if (requestCode == PHOTO_REQUEST_CUT) { try { bitmap = data.getParcelableExtra("data"); this.mFace.setImageBitmap(bitmap); boolean delete = tempFile.delete(); System.out.println("delete = " + delete); } catch (Exception e) { e.printStackTrace(); } } super.onActivityResult(requestCode, resultCode, data); } /** * 剪切图片 * * @function: * @author:Jerry * @date:2013-12-30 * @param uri */ private void crop(Uri uri) { // 裁剪图片意图 Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); // 裁剪框的比例,1:1 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // 裁剪后输出图片的尺寸大小 intent.putExtra("outputX", 250); intent.putExtra("outputY", 250); // 图片格式 intent.putExtra("outputFormat", "JPEG"); intent.putExtra("noFaceDetection", true);// 取消人脸识别 intent.putExtra("return-data", true);// true:不返回uri,false:返回uri startActivityForResult(intent, PHOTO_REQUEST_CUT); } //判断是否有Sd card private boolean hasSdcard() { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { return true; } else { return false; } }
布局文件:
<< 4000 span class="hljs-title">LinearLayout 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:orientation="vertical" tools:context=".MainActivity" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="gallery" android:text="获取图库图片" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="camera" android:text="拍照获取图片" /> <ImageView android:id="@+id/iv_image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:onClick="upload" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上传" /> </LinearLayout>
相关文章推荐
- android studio解决导包冲突
- Android ListView基础篇
- 【Android】GitHub Android Libraries Top 100 简介
- Android之生成桌面快捷方式
- (转载)Android从按下开机键到启动发生了什么
- Android开发-Android Studio使用问题解决
- View学习笔记(2)-View的坐标系以及View的滑动1
- android-View工作原理(四)view的layout过程
- Android Studio 中关于不能自动创建keystore的解决方法
- android-View工作原理(三)视图大小计算过程(measure过程)
- windows下的Android JNI开发
- iOS及Android 启动页面(即欢迎页面),引导页面,及广告页面的加载
- Android如何只显示一次启动页
- android- View工作原理(二)导致View重建原因
- Android SDK与API的对应关系
- Android中adapter的原理简单说明
- Android Studio 学习笔记 - 创建你第一个应用程序
- Android Studio 学习笔记 - 创建你第一个应用程序
- Google Android开发者文档系列-创建有内容分享特性的应用之设置共享文件
- Android生成桌面快捷图标