[英]Upload Bitmap to Ruby Rails server
I need to send JSON like below: 我需要发送JSON,如下所示:
story = {
:story => {
:uuid => "1234567890",
:title => "Title",
:subtitle => "Subtitle",
:private => true,
:bytes => [
{
:uuid => "1234567890",
:timelineDate => "1970-01-01T00:00:00.000+00:00",
:caption => "Byte 1"
:photo => image
}
}
All work fine without image sending, but with image as Bitmap in JSON server return error 无需发送图像即可正常工作,但将图像作为JSON服务器中的位图返回错误
Request I send in this way: 要求我以这种方式发送:
RestClient client = RestClient.getInstance(this, URL);
client.addHeader("Content-Type", "application/json");
client.addHeader("Authorization", "Token token=" token);
//create JSON from Java objects
client.setJSONString(json);
client.execute(RestClient.RequestMethod.POST);
What type of image must to be to send it to Rails server? 要将什么类型的图像发送到Rails服务器?
Rails server want image like this: Rails服务器想要这样的图像:
#<Rack::Test::UploadedFile:0x00000103ea6330
@content_type="image/jpg",
@original_filename="photo.jpg",
@tempfile=
#<File:/var/folders/1j/8b1khcf57fv1stf87gcylk8c0000gn/T/photo.jpg20140512-3303-1u2nkqo>
Try this way 试试这个
1) Convert Bitmap to Base64 String Like this 1)像这样将位图转换为Base64字符串
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
to encode base64 from byte array use following method 从字节数组编码base64使用以下方法
String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
2) set the value of photo in JSON 2)在JSON中设置照片的值
json.put("photo",encoded);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.