.java

  1 package com.jerry.crop;  2   3 import java.io.File;  4   5 import android.app.Activity;  6 import android.content.Intent;  7 import android.graphics.Bitmap;  8 import android.net.Uri;  9 import android.os.Bundle; 10 import android.os.Environment; 11 import android.provider.MediaStore; 12 import android.view.View; 13 import android.widget.ImageView; 14 import android.widget.Toast; 15  16 public class MainActivity extends Activity { 17  18     private static final int PHOTO_REQUEST_CAREMA = 1;// 拍照 19     private static final int PHOTO_REQUEST_GALLERY = 2;// 从相册中选择 20     private static final int PHOTO_REQUEST_CUT = 3;// 结果 21  22     private ImageView iv_image; 23  24     /* 头像名称 */ 25     private static final String PHOTO_FILE_NAME = "temp_photo.jpg"; 26     private File tempFile; 27  28     @Override 29     protected void onCreate(Bundle savedInstanceState) { 30         super.onCreate(savedInstanceState); 31         setContentView(R.layout.activity_main); 32         this.iv_image = (ImageView) this.findViewById(R.id.iv_image); 33     } 34  35     /* 36      * 从相册获取 37      */ 38     public void gallery(View view) { 39         // 激活系统图库,选择一张图片 40         Intent intent = new Intent(Intent.ACTION_PICK); 41         intent.setType("image/*"); 42         // 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_GALLERY 43         startActivityForResult(intent, PHOTO_REQUEST_GALLERY); 44     } 45  46     /* 47      * 从相机获取 48      */ 49     public void camera(View view) { 50         // 激活相机 51         Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 52         // 判断存储卡是否可以用,可用进行存储 53         if (hasSdcard()) { 54             tempFile = new File(Environment.getExternalStorageDirectory(), 55                     PHOTO_FILE_NAME); 56             // 从文件中创建uri 57             Uri uri = Uri.fromFile(tempFile); 58             intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); 59         } 60         // 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_CAREMA 61         startActivityForResult(intent, PHOTO_REQUEST_CAREMA); 62     } 63  64     /* 65      * 剪切图片 66      */ 67     private void crop(Uri uri) { 68         // 裁剪图片意图 69         Intent intent = new Intent("com.android.camera.action.CROP"); 70         intent.setDataAndType(uri, "image/*"); 71         intent.putExtra("crop", "true"); 72         // 裁剪框的比例,1:1 73         intent.putExtra("aspectX", 1); 74         intent.putExtra("aspectY", 1); 75         // 裁剪后输出图片的尺寸大小 76         intent.putExtra("outputX", 250); 77         intent.putExtra("outputY", 250); 78  79         intent.putExtra("outputFormat", "JPEG");// 图片格式 80         intent.putExtra("noFaceDetection", true);// 取消人脸识别 81         intent.putExtra("return-data", true); 82         // 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_CUT 83         startActivityForResult(intent, PHOTO_REQUEST_CUT); 84     } 85  86     /* 87      * 判断sdcard是否被挂载 88      */ 89     private boolean hasSdcard() { 90         if (Environment.getExternalStorageState().equals( 91                 Environment.MEDIA_MOUNTED)) { 92             return true; 93         } else { 94             return false; 95         } 96     } 97  98     @Override 99     protected void onActivityResult(int requestCode, int resultCode, Intent data) {100         if (requestCode == PHOTO_REQUEST_GALLERY) {101             // 从相册返回的数据102             if (data != null) {103                 // 得到图片的全路径104                 Uri uri = data.getData();105                 crop(uri);106             }107 108         } else if (requestCode == PHOTO_REQUEST_CAREMA) {109             // 从相机返回的数据110             if (hasSdcard()) {111                 crop(Uri.fromFile(tempFile));112             } else {113                 Toast.makeText(MainActivity.this, "未找到存储卡,无法存储照片!", 0).show();114             }115 116         } else if (requestCode == PHOTO_REQUEST_CUT) {117             // 从剪切图片返回的数据118             if (data != null) {119                 Bitmap bitmap = data.getParcelableExtra("data");120                 this.iv_image.setImageBitmap(bitmap);121             }122             try {123                 // 将临时文件删除124                 tempFile.delete();125             } catch (Exception e) {126                 e.printStackTrace();127             }128 129         }130 131         super.onActivityResult(requestCode, resultCode, data);132     }133 }

.xml

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" 6     tools:context=".MainActivity" > 7  8     <Button 9         android:layout_width="wrap_content"10         android:layout_height="wrap_content"11         android:onClick="gallery"12         android:text="获取图库图片" />13     <Button14         android:layout_width="wrap_content"15         android:layout_height="wrap_content"16         android:onClick="camera"17         android:text="拍照获取图片" />18 19     <ImageView20         android:id="@+id/iv_image"21         android:layout_width="wrap_content"22         android:layout_height="wrap_content" />23 24 </LinearLayout>

效果图:

更多相关文章

  1. android卡片布局CardView
  2. Android获取 应用程序大小,数据大小,缓存大小
  3. Android(安卓)获取基站信息
  4. android手机两种方式获取IP地址
  5. android 设置时区
  6. Android(安卓)获取通讯录信息
  7. Android获取apk的版本及包名等信息
  8. Android(安卓)异步任务加载图片代码
  9. Android获取本机Mac地址及IP地址方法

随机推荐

  1. Android关于网络连通状况的系统广播
  2. Android Broadcast(广播)简单样例
  3. 初学Andriod之跑马灯属性设置
  4. 最全的 Android 生命周期图
  5. android 输入法出现挤压屏幕
  6. Android手势下拉抽屉效果
  7. Android--取出SDcard卡上指定后缀名的文
  8. Android连续点击两次返回键退出App
  9. Android SDK 2.1 install
  10. Android磨砂效果