json 用base64 编码接收图片

qq_40666749 2017-10-24 10:33:17
if (mSocket != null) {


fi = mSocket.getInputStream();

byte[] bytes = new byte[256];

// TODO
int count;
if ((count = fi.read(bytes)) != -1) {

final String s = new String(bytes, 0, count, "utf-8");
JSONObject jsonObject = new JSONObject(s);
jsonText = "文字:" + jsonObject.getString("jsonText");
Log.d("wenzi", jsonText);
jsonImage = jsonObject.getString("image");
jsonImgPath = jsonObject.getString("imgPath");
Log.d(TAG, jsonImage.toString().length() + "");

byte[] b;

if (jsonImage != null) {
try {
FileOutputStream fileOutputStream = new FileOutputStream(jsonImgPath);
b = new sun.misc.BASE64Decoder().decodeBuffer(jsonImage);
for (int i = 0; i < b.length; i++) {
if (b[i] < 0) {
b[i] += 256;
}
}
int len = 0;



fileOutputStream.write(b);

mBitMap = BitmapFactory.decodeByteArray(b, 0, b.length);//将byte[]转成bitmap
fileOutputStream.flush();
fileOutputStream.close();


} catch (IOException e) {
e.printStackTrace();
Log.d("strToImg", e.getMessage());
}
}


Log.i(TAG, "received bytes " + s);

runOnUiThread(new Runnable() {
@Override
public void run() {
mTextView.setText(jsonText);
mImageView.setImageBitmap(mBitMap);

}
});
}




运行出现Unterminated string at character 256 of {"image":"\/9j... 的异常,求指点
...全文
893 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
rightorwrong 2017-10-30
  • 打赏
  • 举报
回复
是不是 转义字符出了问题

2,543

社区成员

发帖
与我相关
我的任务
社区描述
专题开发/技术/项目 多媒体/流媒体开发
社区管理员
  • 多媒体/流媒体开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧