我正在上传字符串位图作为"用户个人资料图像"。使用 php 上传到服务器都很好,从服务器下载也很好。问题是当我查看位图字符串时,我看到小箭头,我不知道为什么会发生这种情况。
我正在发送的字符串位图(后期编码):1
和我正在接收的字符串位图(预解码): [2] 我把它粘贴到COMENTS中,因为我是新来的,他们不让我放超过1个链接
我想也许我无法从 php 读取字符串。如果需要,我可以将代码粘贴到我执行的位置。
谢谢你们的帮助!
[编辑]
从 PHP 接收我的字符串的代码:
StringBuilder sb = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
sb.append(line + "");
}
line = sb.toString();
一旦我用逗号分隔行:(我收到像"name":"john","age"="5",...,"bitstringImag"="/9j/4AA.." )
我正在获取位图字符串值:
String[] bitmstrIn = kvPairs[5].split(":"); //separating the key from the value
String[] bitmstrIn2 = bitmstrIn[1].split("''}"); //erasing last key in the String
String bitStr = bitmstrIn2[0].replaceAll("'"", ""); //removing the added (i dont know why) backslash.
String biStrFin = bitStr.replaceAll("''''","");//removing the added (i dont know why) backslash.
而 bitStrFin 中的结果是我粘贴到评论中的结果。
谢谢!
我实现了类似的方法,使用服务器端的bin2hex函数从Android上的PHP服务器接收图像。让我们看看代码:
获得 JSON 的十六进制字符串后,我们将其转换为 ByteArray:
/**
* Converts a hexadecimal string to byte array.
*
* @param s String to convert.
* @return The result of conversion.
*/
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
然后从 ByteArray 创建一个新的位图:
/**
* Decode an immutable bitmap from the specified byte array.
*
* @param data byte array of compressed image data
* @param offset offset into imageData for where the decoder should begin parsing.
* @param length the number of bytes, beginning at offset, to parse
* @return The decoded bitmap, or null if the image could not be decoded.
*/
BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
可以使用 imageView.setImageBitmap(Bitmap)
方法将结果位图加载到 ImageView 中。