web-dev-qa-db-ja.com

API 29で廃止されたgetBitmap Java android

Androidアプリケーションを開発しています。ユーザーにギャラリーから画像を選択してもらいたいのですが、API 29ではgetBitmapが非推奨になっています。

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

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