简体   繁体   English

在API 29 java android中不推荐使用getBitmap

[英]getBitmap deprecated in API 29 java android

I am developing an android application I want user to select an image from gallery. 我正在开发一个Android应用程序,我希望用户从库中选择一个图像。 But I have seen that getBitmap is deprecated on API 29 但我已经看到在API 29上不推荐使用getBitmap

I have tried ImageDecoder.decodeBitmap(Imagedecoder.createSource()) but it crashes the application 我试过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();
                }
            }
        }

I am not getting that why app is crashing. 我不知道app崩溃的原因。

here is crash log 这是崩溃日志

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

From Documentation of public static Bitmap getBitmap (ContentResolver cr, Uri url) 来自公共静态位图getBitmap的文档(ContentResolver cr,Uri url)

This method was deprecated in API level 29. loading of images should be performed through ImageDecoder#createSource(ContentResolver, Uri) , which offers modern features like PostProcessor . 此方法在API级别29中已弃用。应通过ImageDecoder#createSource(ContentResolver,Uri)执行图像加载,后者提供PostProcessor等现代功能。


Example algorithm can be 算例可以是

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未指定大小的 Android ThumbnailUtils.createVideoThumbnail 在 API 级别 29 中已弃用 - Android ThumbnailUtils.createVideoThumbnail with no Size specified is deprecated in API level 29 Android Studio - getBitmap 不工作。 返回 null (Java) - Android Studio - getBitmap not working. Returns null (Java) Telephonymanager.EXTRA_INCOMING_NUMBER 在 API 级别 29 中已弃用 - Telephonymanager.EXTRA_INCOMING_NUMBER is deprecated in API level 29 如何在 API 29 中不推荐使用 getExternalStorageDirectory 时读取或写入文件? - How to read or write file as getExternalStorageDirectory is deprecated in API 29? Android 10:API 29 - android.media.MediaMetadataRetriever.setDataSource 处的 java.lang.IllegalArgumentException - Android 10 : API 29 - java.lang.IllegalArgumentException at android.media.MediaMetadataRetriever.setDataSource 来自TextureView的Android LibVLC getBitmap - Android LibVLC getBitmap from a TextureView Android Studio EditText 未出现在预览 API 29 中 - Android Studio EditText not Appearing in preview API 29 GPS 在 android 工作室中的状态 - API 级别 29 - GPS status in android studio - API level 29 在启动时打开 android 服务或活动 api 29 - Open android service or activity on boot api 29 Android Studio-Webview API级别-29问题 - Android Studio - Webview Api level - 29 problem
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM