android 开启本地相册选择图片并返回显示
2015-04-23 19:21
537 查看
.java
.xml
效果图:
![](http://images.cnitblog.com/blog2015/616067/201504/231925236258391.png)
package com.jerry.crop; import java.io.File; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private static final int PHOTO_REQUEST_CAREMA = 1;// 拍照 private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择 private static final int PHOTO_REQUEST_CUT = 3;// 结果 private ImageView iv_image; /* 头像名称 */ 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.iv_image = (ImageView) this.findViewById(R.id.iv_image); } /* * 从相册获取 */ public void gallery(View view) { // 激活系统图库,选择一张图片 Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("image/*"); // 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_GALLERY startActivityForResult(intent, PHOTO_REQUEST_GALLERY); } /* * 从相机获取 */ public void camera(View view) { // 激活相机 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); // 判断存储卡是否可以用,可用进行存储 if (hasSdcard()) { tempFile = new File(Environment.getExternalStorageDirectory(), PHOTO_FILE_NAME); // 从文件中创建uri Uri uri = Uri.fromFile(tempFile); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); } // 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_CAREMA startActivityForResult(intent, PHOTO_REQUEST_CAREMA); } /* * 剪切图片 */ 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); // 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_CUT startActivityForResult(intent, PHOTO_REQUEST_CUT); } /* * 判断sdcard是否被挂载 */ private boolean hasSdcard() { if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { return true; } else { return false; } } @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_CAREMA) { // 从相机返回的数据 if (hasSdcard()) { crop(Uri.fromFile(tempFile)); } else { Toast.makeText(MainActivity.this, "未找到存储卡,无法存储照片!", 0).show(); } } else if (requestCode == PHOTO_REQUEST_CUT) { // 从剪切图片返回的数据 if (data != null) { Bitmap bitmap = data.getParcelableExtra("data"); this.iv_image.setImageBitmap(bitmap); } try { // 将临时文件删除 tempFile.delete(); } catch (Exception e) { e.printStackTrace(); } } super.onActivityResult(requestCode, resultCode, data); } }
.xml
<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" /> </LinearLayout>
效果图:
![](http://images.cnitblog.com/blog2015/616067/201504/231925236258391.png)
![](http://images.cnitblog.com/blog2015/616067/201504/231926084377699.png)
相关文章推荐
- android 开启本地相册选择图片并返回显示
- android 开启本地相册选择图片并返回显示
- 史上最强Android 开启照相或者是从本地相册选中一张图片以后先裁剪在保存并显示的讲解附源码i
- 史上最强Android 开启照相或者是从本地相册选中一张图片以后先裁剪在保存并显示的讲解附源码i
- 史上最强Android 开启照相或者是从本地相册选中一张图片以后先裁剪在保存并显示的讲解附源码i
- 史上最强Android 开启照相或者是从本地相册选中一张图片以后先裁剪在保存并显示的讲解附源码
- Android点击图片显示大图,长按保存网络图片到本地相册,单击返回。(GestureDetector)(RxJava)(Easypermissions)(AlertView)
- android保存图片到本地并可以在相册中显示出来
- android选择本地相册或拍照更换软件背景图片
- Android-ImageView显示选择本地图片或拍照
- Android中ImageView实现选择本地图片并显示功能
- Android-从相册中选择图片显示出来
- android保存图片到本地并可以在相册中显示出来
- Android调用系统相册和相机选择图片并显示在imageview中
- Android开发——相册拍照_02.将拍照得到或相册中选择的图片显示在ImageView中
- android 客户端通过选择本地相册和拍照上传本地图片到服务器端
- Android从相册中选择图片显示出来
- android 打开本地相册,并将选择的照片显示到当前界面
- Android 调用系统相册选择图片并显示
- Android实现保存图片到本地并在相册中显示