I need to create an alert dialog that picks photos from either the camera or gallery and then save them to firebase storage and database. However I don't know how to convert this bitmap to uri and can't get image uri from the camera action result. Thanks in advanvce!
activityResultLauncher1 = registerForActivityResult(new
ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result)
{
if(result.getResultCode() == RESULT_OK && result.getData() != null)
{
Bundle bundle = result.getData().getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");
profilePic.setImageBitmap(bitmap);
}
}
});
I solved it by keeping an URL to my newly created image before sending the intent.
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getContext().getPackageManager()) != null) {
File photoFile = createImageFile();
// I save it to viewModel for example
mViewModel.setPhotoUri(photoFile.getAbsolutePath());
Uri photoURI = FileProvider.getUriForFile(getContext(),
"my.app.id",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
// after photo being made
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// ...
BitmapFactory.decodeFile(mViewModel.getPhotoURI(), bmOptions);
// ...
}
Though I think it's not the most sophisticated way, as onActivityResult
is deprecated.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.