在 Android 中将字节 [] 显示到 ImageView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3545493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display byte[] to ImageView in Android
提问by firnnauriel
Is it possible to do this? I'm reading an XML file that has the Base64 string of an image. I'm planning to use Base64.decode to have the byte array of the image string. I'm stuck though on how to use it in an ImageView. Do i have to create a 'drawable' class first then set it to ImageView's src property?
是否有可能做到这一点?我正在读取一个包含图像 Base64 字符串的 XML 文件。我打算使用 Base64.decode 来获得图像字符串的字节数组。我一直在思考如何在 ImageView 中使用它。我是否必须先创建一个“drawable”类,然后将其设置为 ImageView 的 src 属性?
Thanks!
谢谢!
回答by RobCroll
In case anyone else stumbles across this question, here is the code
如果其他人偶然发现这个问题,这里是代码
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.widget.ImageView;
public class ModelAssistant {
public static void setImageViewWithByteArray(ImageView view, byte[] data) {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
view.setImageBitmap(bitmap);
}
}
回答by Romain Guy
You can use BitmapFactory.decodeByteArray() to perform the decoding.
您可以使用 BitmapFactory.decodeByteArray() 来执行解码。
回答by Chinmoy
// Convert bytes data into a Bitmap
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
ImageView imageView = new ImageView(ConversationsActivity.this);
// Set the Bitmap data to the ImageView
imageView.setImageBitmap(bmp);
// Get the Root View of the layout
ViewGroup layout = (ViewGroup) findViewById(android.R.id.content);
// Add the ImageView to the Layout
layout.addView(imageView);
We convert our byte data into a Bitmap using Bitmap.decodeByteArray() and then set that to a newly created ImageView.
我们使用 Bitmap.decodeByteArray() 将我们的字节数据转换为 Bitmap,然后将其设置为新创建的 ImageView。
回答by Tarun Umath
byte[] pic = intent.getByteArrayExtra("pic");` capturedImage = (ImageView) findViewById(R.id.capturedImage);`Bitmap bitmap = BitmapFactory.decodeByteArray(pic, 0, pic.length);` Bitmap bitmap1 = Bitmap.createScaledBitmap(bitmap,capturedImage.getWidth(),capturedImage.getHeight(),true);` capturedImage.setImageBitmap(bitmap1);`