Android Java Warning: get(String) in BaseBundle has been deprecated

35 Views Asked by At

At "Bitmap tmp = (Bitmap) extras.get("data");" in the following I get warning: [deprecation] get(String) in BaseBundle has been deprecated:

startActivityForResult = registerForActivityResult(
            new ActivityResultContracts.StartActivityForResult(),
            result -> {
                if (result.getResultCode() == Activity.RESULT_OK) {
                    Intent data = result.getData();
                    if (data != null) {
                        // There are no request codes
                        Bundle extras = data.getExtras();
                        if (extras != null) {
                            Bitmap tmp = (Bitmap) extras.get("data");
                            if (tmp != null) {
                                encodeBitmapAndSaveToFirebase(tmp);
                                imageBtnRotateProfileImage.setVisibility(View.VISIBLE);
                                imageBtnDeleteProfileImage.setVisibility(View.VISIBLE);
                            }
                        }
                    }
                }
            }
    );

Tried this too but also deprecated Bitmap tmp = (Bitmap) data.getParcelableExtra("data");

I know I should use a specific type-safe method, but how?

1

There are 1 best solutions below

0
robg On

Solution as instructed by Edric

if (extras != null) {
    Bitmap tmp;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) {
        tmp = (Bitmap) data.getParcelableExtra("data", Bitmap.class);
    } else {
        tmp = (Bitmap) androidx.core.os.BundleCompat.getParcelable(extras, "data", Bitmap.class);
    }
    if (tmp != null) {
        encodeBitmapAndSaveToFirebase(tmp);
        imageBtnRotateProfileImage.setVisibility(View.VISIBLE);
        imageBtnDeleteProfileImage.setVisibility(View.VISIBLE);
    }
}