How to create a selector to choose between Camera, Gallery and Photos in Android Studio?

Asked

Viewed 259 times

-1

Speak Devs! I already gave a search, but I could not find something that would help me, anyway... I would like to create a menu similar to that of the image below.

inserir a descrição da imagem aqui

I can get camera and gallery normally, but one function separate from the other! I would like to press to select the photo to have this selection bar for the user to choose where he wants to upload the image.

  public void abrirCamera(){
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(takePictureIntent, 1);
    }

    public void abrirGaleria(){
        intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Selecione uma imagem"), 2);
    }
  • This may help you: https://github.com/soarcn/BottomSheet

  • It’s not quite what I need, but it can help me! Thank you.

  • 1

    I wanted to know why the guy gave Downlike on the question and said nothing about the mistake. Lack of respect with beginners!

  • is one of the things that happens most on the site. Unfortunately.

  • It always happens, Marcos

1 answer

3


Whoa, that’s all right!?

Follow a code that does exactly what you need. When you click the button call the method below.

public Intent getPickImageChooserIntent()
{
    // Determine Uri of camera image to save.
    Uri outputFileUri = getCaptureImageOutputUri();

    List<Intent> allIntents = new ArrayList<>();
    PackageManager packageManager = getPackageManager();

    // collect all camera intents
    Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
    for (ResolveInfo res : listCam)
    {
        Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(res.activityInfo.packageName);
        if (outputFileUri != null)
        {
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        }
        allIntents.add(intent);
    }

    // collect all gallery intents
    Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
    galleryIntent.setType("image/*");
    List<ResolveInfo> listGallery = packageManager.queryIntentActivities(galleryIntent, 0);
    for (ResolveInfo res : listGallery)
    {
        Intent intent = new Intent(galleryIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(res.activityInfo.packageName);
        allIntents.add(intent);
    }

    // the main intent is the last in the list (fucking android) so pickup the useless one
    Intent mainIntent = allIntents.get(allIntents.size() - 1);
    for (Intent intent : allIntents)
    {
        if (intent.getComponent().getClassName().equals("com.android.documentsui.DocumentsActivity"))
        {
            mainIntent = intent;
            break;
        }
    }
    allIntents.remove(mainIntent);

    // Create a chooser from the main intent
    Intent chooserIntent = Intent.createChooser(mainIntent, "Foto do perfil");

    // Add all other intents
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, allIntents.toArray(new Parcelable[allIntents.size()]));

    return chooserIntent;
}

The rest of you will work onActivityResult, which I assume is already ok.

Hugs!

  • It helped a lot! Thank you. I also managed to do it using the Cropper library. If anyone has questions about this same subject.

  • Face the part of removing image are you there? I’m having a little headache to add this part to remove the image!

  • And how do I launch startActivityForResult(chooserIntent, TIRAR_FOTO_INTENT_CODE); or gallery to differentiate? in onActivityResult?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.