Send image as json entry android

后端 未结 4 2220
伪装坚强ぢ
伪装坚强ぢ 2021-02-11 08:59

I have a requirement where, i am sending a json file to the server and the parsing happens at the server side. I have created the entries to the json file, now i want to store a

4条回答
  •  抹茶落季
    2021-02-11 09:52

    Try base64-encoding the image (like below, where the Uri is your Image - but beware: ImageView has no Getter for the ImageUri, so you have to store it by yourself!):

    Uri uri = (Uri) extras.getParcelable(Intent.EXTRA_STREAM);
    
    ContentResolver cr = getContentResolver();
    InputStream is = cr.openInputStream(uri);
    
    byte[] data = getBytesFromFile(is);
    
    byte[] encoded_data = Base64.encodeBase64(data);
    data_string = new String(encoded_data);
    

    Now you have an base64-encoded String data_string that you can send with your JSON request. On the server-side you just have to decode the String and save the picture.

提交回复
热议问题