jae*_*ina 5 android gradle kotlin
我onActivityResult
不工作,因为getBitmap
已弃用,是否还有其他代码可以实现此目的?
这是需要更改的代码,有什么建议吗?
val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, selectedPhotoUri)
Run Code Online (Sandbox Code Playgroud)
在getBitmap
越过并说,它已过时
Ali*_*Ali 30
这对我有用,
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()
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 17
这在java中对我很有效
ImageDecoder.Source source = ImageDecoder.createSource(this.getContentResolver(), pictureUri);
Bitmap bitmap = ImageDecoder.decodeBitmap(source);
Run Code Online (Sandbox Code Playgroud)
Has*_*sef 12
您可以使用:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
查看官方文档:
此方法在 API 级别 29 中已弃用。图像的加载应通过 执行
ImageDecoder#createSource(ContentResolver, Uri)
,它提供了后处理器等现代功能。
很明显,该getBitmap
API 不适用于最新的 Android SDK - 29。所以,这对我有用
Uri contentURI = data.getData();
try {
imageView.setImageURI(contentURI);
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
如果这对你们中的任何人都不起作用,请告诉我,是否还有其他选择!
小智 6
您可以使用此代码来创建位图
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();
}
}
Run Code Online (Sandbox Code Playgroud)