https://kotlinlang.org logo
#android
Title
# android
a

Adonis Hart

10/04/2023, 9:25 AM
Hello I have issue with
ActivityResultContracts
. I am trying to take photo and save it to the backend database. Thus I have to invoke the camera intent in my activity code, but during this process, I got stuck. The method
startActivityForResult
has already been deprecated, thus I have to use the
registerActivityForResult()
. So I tried the below code, but the
ActivityResultContracts
cannot not be found anywhere. I would be grateful if anyone has similar experience with this issue. 🙂
Copy code
ActivityResultLauncher<Intent> cameraLauncher = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        result -> {
            if (result.getResultCode() == RESULT_OK) {
                Intent data = result.getData();
                Bitmap photo = (Bitmap) data.getExtras().get("data");

                // Save the photo to external storage
                File photoFile = new File(Environment.getExternalStorageDirectory(), "photo.jpg");
                try {
                    FileOutputStream fos = new FileOutputStream(photoFile);
                    photo.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                // Upload the photo file to the backend database using appropriate APIs or libraries
                // Implement your backend integration logic here
            }
        });
stackoverflow 1
not kotlin but kotlin colored 1
k

kenkyee

10/04/2023, 12:47 PM
No need to DM...this was tagged because this channel is for issues with Kotlin problems on Android. Yours is just an Android problem...see channel description.
Anyway @Adonis Hart this channel speaks Kotlin, not Java.