您的位置:首页 > 移动开发 > Android开发

android 开启本地相册选择图片并返回显示

2017-02-13 14:49 513 查看
.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     <Button
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:onClick="camera"
17         android:text="拍照获取图片" />
18
19     <ImageView
20         android:id="@+id/iv_image"
21         android:layout_width="wrap_content"
22         android:layout_height="wrap_content" />
23
24 </LinearLayout>


 

效果图:



 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐