Android显示相册图片和相机拍照
Posted sysu_huangwei
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android显示相册图片和相机拍照相关的知识,希望对你有一定的参考价值。
首先看最重要的MainActive类:
1 public class MainActivity extends AppCompatActivity { 2 3 private final int FROM_ALBUM = 1;//表示从相册获取照片 4 private final int FROM_CAMERA = 2;//表示从相机获取照片 5 private ImageView imageView; 6 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.activity_main); 11 } 12 13 // 打开相册 14 public void onClickAlbum(View view){ 15 Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 16 startActivityForResult(intent, FROM_ALBUM); 17 } 18 19 // 打开相机 20 public void onClickCamera(View view){ 21 Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 22 startActivityForResult(intent, FROM_CAMERA); 23 } 24 25 @Override 26 protected void onActivityResult(int requestCode, int resultCode, Intent data){ 27 28 //从相册返回 29 if(requestCode == FROM_ALBUM && resultCode == Activity.RESULT_OK && data != null){ 30 imageView = (ImageView)findViewById(R.id.imageView); 31 Uri imageUri = data.getData(); 32 ContentResolver cr = this.getContentResolver(); 33 try { 34 Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(imageUri)); 40 imageView.setImageBitmap(bitmap); 41 }catch (FileNotFoundException e){ 42 Log.e("Exception", e.getMessage(), e); 43 } 44 } 45 46 //从相机返回 47 if(requestCode == FROM_CAMERA && resultCode == Activity.RESULT_OK && data != null){ 48 imageView = (ImageView)findViewById(R.id.imageView); 49 Bitmap photo = (Bitmap) data.getExtras().get("data"); 50 51 imageView.setImageBitmap(photo); 52 } 53 54 super.onActivityResult(requestCode, resultCode, data); 55 } 56 57 }
上面两个按钮的处理函数名称在布局中定义,布局如下:两个button(一个打开相册,一个打开相机),一个imageview
1 <?xml version="1.0" encoding="utf-8"?> 2 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context="com.meitu.graydemo.MainActivity"> 8 9 <LinearLayout 10 android:layout_width="368dp" 11 android:layout_height="wrap_content" 12 android:orientation="vertical" 13 tools:layout_editor_absoluteY="0dp" 14 tools:layout_editor_absoluteX="8dp"> 15 16 <LinearLayout 17 android:id="@+id/buttonLayout" 18 android:layout_width="fill_parent" 19 android:layout_height="wrap_content" 20 android:orientation="horizontal"> 21 22 <Button 23 android:id="@+id/button" 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:onClick="onClickAlbum" 27 android:text="打开相册" 28 tools:layout_editor_absoluteX="16dp" 29 tools:layout_editor_absoluteY="16dp" /> 30 31 <Button 32 android:id="@+id/button2" 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:onClick="onClickCamera" 36 android:text="打开相机" 37 tools:layout_editor_absoluteX="280dp" 38 tools:layout_editor_absoluteY="16dp" /> 39 </LinearLayout> 40 41 <ImageView 42 android:id="@+id/imageView" 43 android:layout_width="wrap_content" 44 android:layout_height="wrap_content" 45 app:srcCompat="@android:color/holo_blue_bright" 46 tools:layout_editor_absoluteX="16dp" 47 tools:layout_editor_absoluteY="48dp" /> 48 49 50 </LinearLayout> 51 52 53 54 55 </android.support.constraint.ConstraintLayout>
以上是关于Android显示相册图片和相机拍照的主要内容,如果未能解决你的问题,请参考以下文章
Flutter从相册选择图片和相机拍照(image_picker)