gpt4 book ai didi

java - 看来 ImageView.setImageBitmap 不起作用

转载 作者:行者123 更新时间:2023-12-02 10:15:29 28 4
gpt4 key购买 nike

我有一个 Activity ,您可以在其中绘制位图,并使用 Intent 将其发送到下一个 Activity 并将其放入其中的 ImageView 中。由于某种原因,它不会产生错误,但也不会将图像设置为应有的样子。当宽度和高度不兼容时,这会成为问题吗?

ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

Intent i = new Intent(getApplicationContext(), PrintActivity.class);
i.putExtra("bitmap", byteArray);
startActivity(i);

我只想提一下,此方法适用于其他 Activity ,因为我多次跨 Activity 发送位图。

获取 Intent 的 Activity :

img = findViewById(R.id.img);

byte[] byteArray = getIntent().getByteArrayExtra("bitmap");
if (byteArray.length > 0) {
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); // In a different thread this seemed to help the OP but it didn't in my case
img.setImageBitmap(bmp);

我还尝试将位图保存到图库,但出现了多个错误,其中一些错误表明位图为空。虽然我不明白这是怎么回事,因为它是 Canvas 的位图。我用这个获取它:

public Bitmap getBitmap() {
return mBitmap;
}

mbitmap 是用来在 Canvas 上书写的:

canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

我做错了什么?

最佳答案

我看到您通过压缩为字节数组来移动位图,然后再次解码它们。请注意,BitMaps 实现了 Parcelable,这很好,因为您可以使用

将 Parcelable 直接放入 Intent 中
Intent intentions = new Intent(this, someActivity.class);
intentions.putExtra("parcelable_photo", mPhoto);

获取包裹

Intent received = getIntent();
Bitmap mPhoto = (Bitmap) received.getExtras().getParcelable("parcelable_photo");

这应该比您当前的方法更快、更少的打字工作。

要在 ImageView 中设置此位图,请执行以下操作:

ImageView mImg = findViewById(R.id.img_id);
img.setImageBitmap(mPhoto);

这是source对于setImageBitmap()

我不知道ImageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null),但我希望上述方法能够解决问题。

如果问题仍然存在,请随时发表评论

关于java - 看来 ImageView.setImageBitmap 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54712503/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com