在 API 29 java android 中不推荐使用 getBitmap
Posted
技术标签:
【中文标题】在 API 29 java android 中不推荐使用 getBitmap【英文标题】:getBitmap deprecated in API 29 java android 【发布时间】:2019-11-29 14:29:26 【问题描述】:我正在开发一个 android 应用程序,我希望用户从图库中选择图像。但我看到 getBitmap
在 API 29 上已被弃用
我尝试过ImageDecoder.decodeBitmap(Imagedecoder.createSource())
,但它使应用程序崩溃
if (requestCode == GALLERY)
if (data != null)
Uri contentURI = data.getData();
try
if (contentURI!=null)
bitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(getContentResolver(), contentURI));
Toast.makeText(getApplicationContext(), "Image Saved!", Toast.LENGTH_SHORT).show();
pic.setImageBitmap(bitmap);
dialog.show();
catch (Exception e)
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Failed!", Toast.LENGTH_SHORT).show();
我不明白应用程序崩溃的原因。
这是崩溃日志
java.lang.NoClassDefFoundError: Failed resolution of: Landroid/graphics/ImageDecoder;
at com.example.myapplication.ProfileView.onActivityResult(ProfileView.java:313)
at android.app.Activity.dispatchActivityResult(Activity.java:6223)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3632)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3679)
at android.app.ActivityThread.access$1300(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1358)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5354)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.graphics.ImageDecoder" on path: DexPathList[[zip file "/data/app/com.example.myapplication-1/base.apk", zip file "/data/app/com.example.myapplication-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.myapplication-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.myapplication-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.myapplication-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.myapplication-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.myapplication-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.myapplication-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.myapplication-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.myapplication-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.myapplication-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.myapplication-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at com.example.myapplication.ProfileView.onActivityResult(ProfileView.java:313)
at android.app.Activity.dispatchActivityResult(Activity.java:6223)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3632)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3679)
at android.app.ActivityThread.access$1300(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1358)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5354)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Suppressed: java.lang.ClassNotFoundException: android.graphics.ImageDecoder
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 14 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
【问题讨论】:
“但它使应用程序崩溃”您在这里遇到什么异常? java.lang.NoClassDefFoundError: 解析失败:Landroid/graphics/ImageDecoder; 【参考方案1】:来自public static Bitmap getBitmap (ContentResolver cr, Uri url)的文档
此方法在 API 级别 29 中已弃用。加载图像应 通过ImageDecoder#createSource(ContentResolver, Uri)执行, 它提供了现代功能,例如PostProcessor。
示例算法可以
if (android.os.Build.VERSION.SDK_INT >= 29)
// To handle deprication use
ImageDecoder.decodeBitmap(Imagedecoder.createSource())
else
// Use older version
MediaStore.Images.Media.getBitmap(contentResolver, imageUri)
【讨论】:
是的,我试过这个功能,但它会导致应用程序崩溃 java.lang.NoClassDefFoundError: 解析失败:Landroid/graphics/ImageDecoder; 该错误是因为您正在以下 29 台设备中验证您的代码。这个方法在29已经加入了,连28都没有。所以用这两种方法来处理 不适合我..它给我的结果是:意外错误不支持位图配置:“硬件”以上是关于在 API 29 java android 中不推荐使用 getBitmap的主要内容,如果未能解决你的问题,请参考以下文章
android studio 中的 GPS 状态 - API 级别 29
为啥在 Android 的 Geoloqi API 中不调用 onPushMessageReceived()?