“deprecated” vs “字段要求API级别30(当前最低级别为21)”

huangapple go评论48阅读模式
英文:

"deprecated" vs "Field requires API level 30 (current min is 21)"

问题

如果我写:

bmp.compress(Bitmap.CompressFormat.WEBP_LOSSLESS, 80, image)

Android Studio显示一个错误消息:
> 字段要求API级别为30(当前最低级别为21)
而如果我写:

bmp.compress(Bitmap.CompressFormat.WEBP, 80, image)

会提示:
> WEBP已被弃用。在Java中已弃用。

那我应该这样做吗?:

if (android.os.Build.VERSION.SDK_INT >= 30) {
    bmp.compress(Bitmap.CompressFormat.WEBP_LOSSLESS, 80, image)
} else {
    bmp.compress(Bitmap.CompressFormat.WEBP, 80, image)
}

在这种情况下,我们在Android Studio中定义的目标SDK是否会影响此代码?

英文:

If I write:

bmp.compress(Bitmap.CompressFormat.WEBP_LOSSLESS, 80, image)

Android Studio shows an error message:
> Field requires API level 30 (current min is 21)
And if I write:

bmp.compress(Bitmap.CompressFormat.WEBP, 80, image)

says:
> WEBP is deprecated. Deprecated in Java.

Should I do this?:

if(android.os.Build.VERSION.SDK_INT >= 30) {
    bmp.compress(Bitmap.CompressFormat.WEBP_LOSSLESS, 80, image)
}else{
    bmp.compress(Bitmap.CompressFormat.WEBP, 80, image)
}

In this case, the target SDK that we have defined in Android Studio does not interfere with this code?

答案1

得分: 0

是的,不幸的是,这确实是你必须要做的。Android Studio不会抱怨或干扰。

有许多问题涉及到你所写的完全相同的代码块,例如在如何处理Android中的弃用类以保持兼容性的答案中。

一旦停止支持Android版本低于API级别30,你可以删除这个代码块。

英文:

Yes, that's exactly what you have to, unfortunately. Android studio won't complain or interfere.

There are lots of questions that involve the exact same block that you wrote, for example in the answers to How to deal with deprecated classes in Android to keep compatibility

Once you drop support for Android versions below API level 30, you can remove this block.

huangapple
  • 本文由 发表于 2023年6月29日 13:10:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76578204.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定