知道图片路径
不知道你问题的哪个部分?
服务器端如何把图片转换成base64
字符串?
String filePath = ".......";
Bitmap selectedImage = BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
selectedImage.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
String strBase64=Base64.encodeToString(byteArray, 0);
//下面你只要把这个字符串当成api的返回值,返回给android端就好了
下面是android上如何把base64
的字符串转换成图片(ImageView
默认不能显示base64
)
byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
图片编码64是要把图片io读到内存然后编码然后再把编码后的字串发到客户端?
感觉完全多此一举。
诶。。。读取二进制 转成base64的字符串,像平常传json数据一样传给客户端就行呀。。
不过base64编码会增大体积