Deprecated “getBitmap” with API 29. Any alternative codes?

前端 未结 9 2244
渐次进展
渐次进展 2020-12-15 20:25

My onActivityResult is not working because getBitmap is deprecated, any alternative codes to achieve this?

here are the codes that needs to

9条回答
  •  清歌不尽
    2020-12-15 20:54

    I have created a class for loading a Bitmap from uri:

    public class BitmapResolver {
        private final static String TAG = "BitmapResolver";
    
        @SuppressWarnings("deprecation")
        private static Bitmap getBitmapLegacy(@NonNull ContentResolver contentResolver, @NonNull Uri fileUri){
            Bitmap bitmap = null;
    
            try {
                bitmap = MediaStore.Images.Media.getBitmap(contentResolver, fileUri);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return bitmap;
        }
    
        @TargetApi(Build.VERSION_CODES.P)
        private static Bitmap getBitmapImageDecoder(@NonNull ContentResolver contentResolver, @NonNull Uri fileUri){
            Bitmap bitmap = null;
    
            try {
                bitmap = ImageDecoder.decodeBitmap(ImageDecoder.createSource(contentResolver, fileUri));
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return bitmap;
        }
    
        public static Bitmap getBitmap(@NonNull ContentResolver contentResolver, Uri fileUri){
            if (fileUri == null){
                Log.i(TAG, "returning null because URI was null");
                return null;
            }
    
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P){
                return getBitmapImageDecoder(contentResolver, fileUri);
            } else{
                return getBitmapLegacy(contentResolver, fileUri);
            }
        }
       }
    

    Just to save you some time ...

提交回复
热议问题