Android Java 警告:BaseBundle 中的 get(String) 已被弃用

问题描述 投票:0回答:1

在“位图 tmp = (位图) extras.get(“数据”);”在下面我收到警告:[弃用] BaseBundle 中的 get(String) 已被弃用:

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);
                            }
                        }
                    }
                }
            }
    );

也尝试过这个,但也弃用了 Bitmap tmp = (Bitmap) data.getParcelableExtra("data");

我知道我应该使用特定的类型安全方法,但是如何使用?

java android bundle deprecated
1个回答
0
投票

按照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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.