web-dev-qa-db-fra.com

getBitmap déconseillé dans l'API 29 Java android

Je développe une Android pour Android que je souhaite que l'utilisateur sélectionne une image de la galerie. Mais j'ai vu que getBitmap est déconseillé sur l'API 29

J'ai essayé ImageDecoder.decodeBitmap(Imagedecoder.createSource()) mais ça plante l'application

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

Je ne comprends pas pourquoi l'application se bloque.

voici le journal des plantages

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
8

De la documentation de bitmap public statique getBitmap (ContentResolver cr, Uri url)

Cette méthode est obsolète au niveau de l'API 29. Le chargement des images doit être effectué via ImageDecoder # createSource (ContentResolver, Uri) , qui offre des fonctionnalités modernes comme PostProcessor .


Un exemple d'algorithme peut être

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)
}
11
Pankaj Kumar