Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

registerActivityForResult getting the result from the camera

especially @Muntashir Akon, I'm from "Down Under", please if you could spare some time.

As we all know now startActivityForResult is DEPRECATED, thanks google. How do we get the result from a camera .. here is my code intentData always returns null. Please help this is driving me crazy.

I am possibly using the wrong caller, but I can't seem to find anything on the internet

for @Muntashir Akon, the actual caller is yours ...

    protected final RegisterActivityResultHelper<Intent, 
         ActivityResult> activityLauncher =
            RegisterActivityResultHelper.registerActivityForResult( 
                     this );
RegisterActivityForResult
    ActivityResultLauncher activityResultLauncher =
        registerForActivityResult( new ActivityResultContracts.StartActivityForResult(),
            new ActivityResultCallback()
    {
        @Override
        public void onActivityResult( Object result )
        {
        }
    });
    /////////////////////////////////////////////////////////////////////////////////////

    private void takePhoto()
    {
        String uuid = UUID.randomUUID().toString();
        File outputDir = getCacheDir();
        File file;
        try
        {
            file = File.createTempFile( uuid, ".jpg", outputDir );
        }
        catch( IOException e )
        {
            return;
        }

        Intent intent = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
        Uri uri;
        try
        {
            uri = FileProvider.getUriForFile( Objects.requireNonNull(
                    getApplicationContext()),
                    BuildConfig.APPLICATION_ID + ".fileProvider", file );
        }
        catch( IllegalArgumentException e )
        {
            return;
        }

        intent.putExtra( MediaStore.EXTRA_OUTPUT, uri );
        activityLauncher.launch( intent );
        activityLauncher.setOnActivityResult( result ->
        {
            if( result.getResultCode() == RESULT_CANCELED )
                return;

            Intent intentData = result.getData();
            if( intentData == null )
            {
                return;
            }

            Uri imageUri = intentData.getData();
            Bundle bundle = intentData.getExtras();

            Log.e("URI",imageUri.toString());
            Bitmap bmp = ( Bitmap ) bundle.get( "data" );
        } );
    }

like image 839
HemmaRoyD Avatar asked Nov 14 '25 16:11

HemmaRoyD


1 Answers

Ok, I have the answer and I hope it helps everyone else

Launcher ...

    /////////////////////////////////////////////////////////////////

    ActivityResultLauncher<Uri> takeAPhoto = registerForActivityResult(
        new ActivityResultContracts.TakePicture(), result ->
    {
        if( !result  )
            return;

        Helpers.loadPicIntoGlide( ivItemImage, photoTakenUri );
        etImageName.setText( photoTakenUri.getPath() );
    } );

Caller ...

    /////////////////////////////////////////////////////////////////////////////////////

    private void takePhoto()
    {
        String uuid = UUID.randomUUID().toString();
        File outputDir = getCacheDir();
        File file;
        try
        {
            file = File.createTempFile( uuid, ".jpg", outputDir );
        }
        catch( IOException e )
        {
            return;
        }

        try
        {
            photoTakenUri = FileProvider.getUriForFile( Objects.requireNonNull(
                    getApplicationContext()),
                    BuildConfig.APPLICATION_ID + ".fileProvider", file );
        }
        catch( IllegalArgumentException e )
        {
            return;
        }

        takeAPhoto.launch( photoTakenUri );
    }

    ////////////////////////////////////////////////////////////////////////
like image 86
HemmaRoyD Avatar answered Nov 17 '25 10:11

HemmaRoyD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!