首先看最重要的MainActive类:
public class MainActivity extends AppCompatActivity { private final int FROM_ALBUM = 1;//表示从相册获取照片 private final int FROM_CAMERA = 2;//表示从相机获取照片 private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); textView = (TextView)findViewById(R.id.textView); setContentView(R.layout.activity_main); applyWritePermission();//请求权限 } // 打开相册 public void onClickAlbum(View view){ Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent, FROM_ALBUM); } // 打开相机 public void onClickCamera(View view){ Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, FROM_CAMERA); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data){ //从相册返回 if(requestCode == FROM_ALBUM && resultCode == Activity.RESULT_OK && data != null){ imageView = (ImageView)findViewById(R.id.imageView); textView = (TextView)findViewById(R.id.textView); Uri imageUri = data.getData(); ContentResolver cr = this.getContentResolver(); try { Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(imageUri)); int res = FaceClassified.runClassified(bitmap); imageView.setImageBitmap(bitmap); }catch (FileNotFoundException e){ Log.e("Exception", e.getMessage(), e); } } //从相机返回 if(requestCode == FROM_CAMERA && resultCode == Activity.RESULT_OK && data != null){ imageView = (ImageView)findViewById(R.id.imageView); textView = (TextView)findViewById(R.id.textView); Bitmap photo = (Bitmap) data.getExtras().get("data"); int res = FaceClassified.runClassified(photo); imageView.setImageBitmap(photo); } super.onActivityResult(requestCode, resultCode, data); } private void applyWritePermission() { String permissions1 = Manifest.permission.WRITE_EXTERNAL_STORAGE; String permissions2 = Manifest.permission.READ_EXTERNAL_STORAGE; String permissions3 = Manifest.permission.CAMERA; if (Build.VERSION.SDK_INT >= 23) { int check1 = ContextCompat.checkSelfPermission(this, permissions1); if (check1 != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); } int check2 = ContextCompat.checkSelfPermission(this, permissions2); if (check2 != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1); } int check3 = ContextCompat.checkSelfPermission(this, permissions3); if (check3 != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.CAMERA}, 1); } } } }
上面两个按钮的处理函数名称在布局中定义,布局如下:两个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>