Adonis Hart
10/04/2023, 9:25 AMActivityResultContracts
.
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.
🙂
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
}
});
kenkyee
10/04/2023, 12:47 PMstatmark56
10/04/2023, 1:27 PM