【发布时间】:2021-02-24 12:41:58
【问题描述】:
我从用户那里获取指纹,并使用 glide 为其添加白色背景。问题是我需要获取该图像的字节数组,但我无法获取它,因为保存在 ImageView 中的图像是 VectorDrawable。我无法将其转换回位图,从而转换为字节数组。我尝试在虚拟 png 图像上使用它,如下所示。
获取任何虚拟 PNG 图像
firstIV = findViewById(R.id.firstIV);
secondIV = findViewById(R.id.secondIV);
Bitmap currentBmp = BitmapFactory.decodeResource(getResources(), R.drawable.test_image);
Glide.with(this).load(currentBmp).into(new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(Drawable resource, Transition<? super Drawable> glideAnimation) {
final ShapeDrawable background = new ShapeDrawable();
background.getPaint().setColor(Color.WHITE);
final Drawable[] layers = {background, resource};
firstIV.setImageDrawable(new LayerDrawable(layers));
setToAnotherImageView(firstIV.getDrawable().getIntrinsicWidth(), firstIV.getDrawable().getIntrinsicHeight());
}
});
应用解决方案
private void setToAnotherImageView(int width, int height) {
Log.e(TAG, "onCreate: " + width + ":" + height);
Bitmap convertedBmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(convertedBmp);
firstIV.draw(canvas);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
convertedBmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Bitmap newBmp = BitmapFactory.decodeByteArray(byteArray,0,byteArray.length);
secondIV.setImageBitmap(newBmp);
}
这样做后,我的应用程序崩溃,并出现一个异常,指出矢量可绘制对象无法转换为位图。以下是错误:
本来我想要达到的
最初我想要存储在 ImageView 中的图片的字节数组,以便我可以将该字节数组转换为 WSQ 字节数组并将其发送到我的服务器。我正在尝试通过上面定义的示例来实现这一目标。
【问题讨论】:
-
我无法重现该问题:您在此处发布的第一种方法会导致 NullPointerException,因为您尝试在设置之前访问 imageView1 的可绘制对象(
onResourceReady()只能运行一次当前方法已完成)。所以我将问题行移到onResourceReady()的末尾。现在我得到了一个 ClassCastException,但它与 VectorDrawables 无关。无论如何,我能够使用您在此处发布的方法解决问题,现在我有两个 ImageView,它们或多或少具有相同的可绘制对象(差异可能是由于不同的 scaleTypes)(续) -
(cont) 所以到位图的转换确实奏效了,任务完成了。不幸的是,我不知道你应该改变什么,因为我看不到你目前在做什么。如果您仍需要帮助,请添加更多代码
-
有两个 ImageViews 我用 PNG 文件设置第一个 ImageView 并使用 Glide 为其添加白色背景。将图像设置为第一个 ImageView 后,我想要该 ImageView 的位图并将其设置为我的第二个 ImageView。由于从 VectorDrawable 到 Bitmap 的转换错误,我无法从第一个 ImageView 获取 Bitmap。
-
哪一行出现错误?
-
顺便说一句,我更新了我的答案。
标签: android android-glide android-vectordrawable