Single intent to let user take picture OR pick image from gallery in Android

前端 未结 4 431
广开言路
广开言路 2020-12-07 08:05

I\'m developing an app for Android 2.1 upwards. I want to enable my users to select a profile picture within my app (I\'m not using the contacts framework).

The id

4条回答
  •  甜味超标
    2020-12-07 08:44

    You can try doing something like this:

    // ...
    // Within your enclosing Class
    // ...
    private static final int SELECT_PICTURE = 1;
    
    // ... 
    
    Intent pickIntent = new Intent();
    pickIntent.setType("image/*");
    pickIntent.setAction(Intent.ACTION_GET_CONTENT);
    
    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    
    String pickTitle = "Select or take a new Picture"; // Or get from strings.xml
    Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
    chooserIntent.putExtra
    (
      Intent.EXTRA_INITIAL_INTENTS, 
      new Intent[] { takePhotoIntent }
    );
    
    startActivityForResult(chooserIntent, SELECT_PICTURE);
    

    To see how to handle the activitiy's result, please refer to this question


    Note: a critical point is how to determine whether the camera or gallery was used. That is shown in this code example: https://stackoverflow.com/a/12347567/294884

提交回复
热议问题