API 29已弃用“ getBitmap”。是否还有其他代码?

时间:2019-06-18 14:29:20

标签: android gradle kotlin

我的onActivityResult无法使用,是因为getBitmap已过时,是否有其他替代代码可以实现这一目标?

这是需要更改的代码,有什么建议吗?

val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, selectedPhotoUri)

getBitmap越过并表示已弃用

13 个答案:

答案 0 :(得分:4)

这在Java中对我来说很好

ImageDecoder.Source source = ImageDecoder.createSource(this.getContentResolver(), pictureUri);
Bitmap bitmap = ImageDecoder.decodeBitmap(source);

答案 1 :(得分:4)

这对我有用,

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if(requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {

            val selectedPhotoUri = data.data
            try {
                selectedPhotoUri?.let {
                    if(Build.VERSION.SDK_INT < 28) {
                        val bitmap = MediaStore.Images.Media.getBitmap(
                            this.contentResolver,
                            selectedPhotoUri
                        )
                        imageView.setImageBitmap(bitmap)
                    } else {
                        val source = ImageDecoder.createSource(this.contentResolver, selectedPhotoUri)
                        val bitmap = ImageDecoder.decodeBitmap(source)
                        imageView.setImageBitmap(bitmap)
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

答案 2 :(得分:3)

检查official doc:

  

此方法在API级别29中已弃用。   图像的加载应通过ImageDecoder#createSource(ContentResolver, Uri)执行,该功能提供了PostProcessor等现代功能。

答案 3 :(得分:3)

您可以使用此代码创建位图

Bitmap bitmap;
if (Build.VERSION.SDK_INT >= 29) {
     ImageDecoder.Source source = ImageDecoder.createSource(getApplicationContext().getContentResolver(), imageUri);
     try {
         bitmap = ImageDecoder.decodeBitmap(source);
     } catch (IOException e) {
         e.printStackTrace();
     }
} else {
     try {
     bitmap = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), imageUri);
     } catch (IOException e) {
          e.printStackTrace();
     }
}

答案 4 :(得分:2)

很明显,getBitmap API不能与最新的Android SDK-29配合使用。因此,这对我有用

Uri contentURI = data.getData();
try {
    imageView.setImageURI(contentURI);
} catch (Exception e) {
    e.printStackTrace();
}

请让我知道这是否对您都不起作用,其他选择是否可以!

答案 5 :(得分:0)

您尝试过吗?

val bitmap = ImageDecoder.createSource(contentResolver, uri)

答案 6 :(得分:0)

  

ImageDecoder.createSource(this.getContentResolver(),pictureUri)

可以正常工作,但是要能够使用此代码,mindSdkVersion应该至少为28。

答案 7 :(得分:0)

您可以使用:

    private fun getCapturedImage(selectedPhotoUri: Uri): Bitmap {
        val bitmap = when {
            Build.VERSION.SDK_INT < 28 -> MediaStore.Images.Media.getBitmap(
                this.contentResolver,
                selectedPhotoUri
            )
            else -> {
                val source = ImageDecoder.createSource(this.contentResolver, selectedPhotoUri)
                ImageDecoder.decodeBitmap(source)
            }
        }

答案 8 :(得分:0)

我创建了一个用于从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);
        }
    }
   }

只是为了节省您一些时间...

答案 9 :(得分:0)

嗨,朋友,您检查 api 设备

var Image_select: String? = null
var bitmap:Bitmap?=null

you show image set

binding?.ImAvator?.setImageURI(data!!.data)


 try {
                    val uri: Uri? = data!!.data
                    bitmap = if(Build.VERSION.SDK_INT>=29){
                        val source: ImageDecoder.Source = ImageDecoder.createSource(requireActivity()
                            .contentResolver, uri!!)
                        ImageDecoder.decodeBitmap(source)
                    } else{
                        MediaStore.Images.Media.getBitmap(requireActivity().contentResolver, uri!!)
                    }
               

                } catch (e: IOException) {
                    e.printStackTrace()
                }

上传图片时

压缩位图发送服务器

 fun Camparse() {
        val size = (bitmap!!.height * (812.0 / bitmap!!.width)).toInt()
        val b = Bitmap.createScaledBitmap(bitmap!!, 812, size, true)
        val by = ByteArrayOutputStream()
        b.compress(Bitmap.CompressFormat.JPEG, 100, by)
        val bytes = by.toByteArray()
        Image_select = Base64.encodeToString(bytes, 0)
    }

答案 10 :(得分:0)

对于 API 级别 29 中已弃用的 MediaStore.Images.Media.getBitmap(),您可以使用以下代码:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == Activity.RESULT_OK) {
        if (requestCode == GALLERY_REQUEST) {
            Uri selectedImage = data.getData();
            try {
                if (Build.VERSION.SDK_INT < 29) {
                    Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), selectedImage);
                    imageView2.setImageBitmap(bitmap);
                } else {
                    ImageDecoder.Source source = ImageDecoder.createSource(getActivity().getContentResolver(), selectedImage);
                    Bitmap bitmap = ImageDecoder.decodeBitmap(source);
                    imageView2.setImageBitmap(bitmap);
                }
            } catch (IOException e) {
                Toast.makeText(getContext(), R.string.error_read_image, Toast.LENGTH_LONG).show();
            }
        }
    }
}

问候。

答案 11 :(得分:0)

尝试使用 ImageDecoder

if b == 'x':

答案 12 :(得分:-1)

此代码适用于我的情况:

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    try {
            when (requestCode) {

                //get the image with camera
                SECTIONCAMERA -> {
                    val imageBitmap = data?.extras?.get("data") as Bitmap
                  ImageView_imagePerfil.setImageBitmap(imageBitmap)

                }
                //get the image in gallery
                SECTIONGALLERY -> {
                    val imageUri = data?.data
                    ImageView_imagePerfil.setImageURI(imageUri) }
            }

    } catch (e: Exception){
             e.printStackTrace()
    }
}
相关问题