簡體   English   中英

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

[英]getBitmap deprecated in API 29 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();
                }
            }
        }

我不知道app崩潰的原因。

這是崩潰日志

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

來自公共靜態位圖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)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM