Android starts the camera to take a picture and returns the picture

  • 2020-10-07 18:52:53
  • OfStack

Please refer to the following code for the specific implementation process:

Simply called the photo taking function of the system under 1

The code is as follows:

// How to take photos


 private void openTakePhoto(){
  /**
  *  It's a good idea to judge before starting the shoot 1 Under the sdcard Whether the available 
  */
  String state = Environment.getExternalStorageState(); // get sdcard Is the status code available 
  if (state.equals(Environment.MEDIA_MOUNTED)){   // If available 
   Intent intent = newIntent("android.media.action.IMAGE_CAPTURE");
   startActivityForResult(intent,TAKE_PHOTO);
  }else {
   Toast.makeText(SettingActivity.this,"sdcard Do not use ",Toast.LENGTH_SHORT).show();
  }
 }

After the shooting, we sure we have as needed to take good photos for 1 some operations (for example, in face, hair band graph state, etc.), then we need to rewrite onActivityResult () method, we take a good photo to get to, we can through the following two ways to get our photos, his plus is encapsulated within our Intent, we only need to take out to:


@Override
 protected void onActivityResult(int requestCode, int resultCode, Intentdata) {
  super.onActivityResult(requestCode, resultCode, data);
  if (data!= null) {
   switch (requestCode) {
    case TAKE_PHOTO: // Take pictures and select 
     // One of two ways   Get the picture taken 
     if (data.getData() != null|| data.getExtras() != null){ // Prevent no results from being returned 
      Uri uri =data.getData();
      if (uri != null) {
       photo =BitmapFactory.decodeFile(uri.getPath()); // To get the pictures 
      }
      if (photo == null) {
       Bundle bundle =data.getExtras();
       if (bundle != null){
        photo =(Bitmap) bundle.get("data");
       } else {
        Toast.makeText(getApplicationContext(), " I can't find the picture ",Toast.LENGTH_SHORT).show();
       }
      }
      // Processing images 
      // Cut out pictures 
     }
     break;

The above code with comments, I believe we can see, what do not understand the place welcome friends to leave a message, I will be in the first time and you get in touch. Thank you very much!


Related articles: