【发布时间】:2016-07-18 06:52:02
【问题描述】:
我想在我的 android 应用程序中显示图像。为此我正在调用我的服务,因为图像采用这种格式:
image:[255,216,255,224,0,16,74,70,73,70,0,1,1,0,0,1,0,1,0,0......].
如何在android中显示此图像。
【问题讨论】:
-
你的问题不清楚
我想在我的 android 应用程序中显示图像。为此我正在调用我的服务,因为图像采用这种格式:
image:[255,216,255,224,0,16,74,70,73,70,0,1,1,0,0,1,0,1,0,0......].
如何在android中显示此图像。
【问题讨论】:
你能解释的更详细一点吗?你的图像是 base 64 格式吗?如果是,那么你必须解码图像并将其显示在 imageview 中。
byte[] b = Base64.decode(previouslyEncodedImage, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
img.setImageBitmap(bitmap);
【讨论】:
使用下面的代码
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
这里的 bytearray 是,你从服务器得到什么
【讨论】:
通知您这称为图像的字节数组,您必须使用 BitmapFactory 类的 decodeByteArray 方法将该数组解码为 Bitmap,例如,
Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, image.length);
【讨论】:
试试这个对我有用
String myString = "[255,216,255....]";
try {
JSONArray arr = new JSONArray(myString);
byte[] myArray = new byte[myString.length()];
for (int i = 0; i < arr.length(); i++) {
myArray[i] = (byte) arr.getInt(i);
}
Bitmap bmp = BitmapFactory.decodeByteArray(myArray, 0, myArray.length);
ImageView image = (ImageView) findViewById(R.id.myImageView);
image.setImageBitmap(bmp);
} catch (JSONException e) {
e.printStackTrace();
}
解释步骤:
Start by converting that string to a JsonArray
Convert all items in it to bytes and add them to an byte[]
Convert the byte array to a bitmap
Set that bitmap on your image
【讨论】: