I'm trying to convert a Bitmap to String that I can include in my multipart HTTP request, I have to construct the request body manually and my problem is that when I convert the Bitmap to byte array and then from byte array to String I seem to lose the integrity of the file structure. How can I convert the Bitmap to String without damaging the file structure?
Bitmap imageBitmap = BitmapFactory.decodeFile(data.getData());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
String s = new String(baos.toByteArray());
You can convert Bitmap to String with Base64 Try this ...
public String convertBitmapToString(final Bitmap pBitmap){
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
pBitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
final byte [] bytes = byteArrayOutputStream.toByteArray();
final String string = Base64.encodeToString(bytes, Base64.DEFAULT);
return string;
}
Or if you can't use base64, try this
try {
String s = new String(baos.toByteArray(), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.