英文:
"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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论