在Android中将jpeg文件作为文本发布

时间:2016-05-29 03:33:58

标签: android string http jpeg

我在Android设备上有照片(jpeg)格式。我没有使用HTTP将文件作为文件发布,而是将其转换为字符串,并使用http将其作为字符串发布到电子表格中。我理解jpeg文件是编码的,并打开它们作为字符串显示有趣的字符。我的问题是,如果我使用http将这些字符作为字符串发送,我可以使用二进制文件编辑器将它们恢复到另一端并将它们保存为jpeg吗?

1 个答案:

答案 0 :(得分:0)

您可以做的一件事是,在客户端:

1.将图像转换为byte数组。

Path path = yourImageFile.toPath();
byte[] byteArray = Files.readAllBytes(path);

2.将byteArray编码为Base64 String

String encodedString = Base64.encodeBase64URLSafeString(byteArray);

3.就是这样,通过http发送。

我不确定二进制文件编辑器是什么意思,但是从服务器端你可以像这样检索图像(如果使用Java):

byte[] decodedByte = decodedBase64.decodeBase64(encodedString);
FileOutputStream out = new FileOutputStream("newPathToYourImage");
out.write(decodedByte);
out.close();