Android 如何将图像转换为base64字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12297381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to convert an image into base64 string
提问by SubbaReddy PolamReddy
I want to convert image to base 64 encode to string. from that to send to server with oma_status-icon
xml format.
我想将图像转换为 base 64 编码为字符串。从那个以oma_status-icon
xml格式发送到服务器。
but I am getting unsupported encoding from the server response....
但是我从服务器响应中得到了不受支持的编码....
is there any other way to convert image to base64 string??
有没有其他方法可以将图像转换为 base64 字符串?
plz..help...
lz..帮助...
my code is:
我的代码是:
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.image);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeBytes(ba);
回答by Nikhil
Please use this code..
请使用此代码..
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.image);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeToString(ba,Base64.DEFAULT);
Please import
请导入
import android.util.Base64;
回答by Nantharupan
// convert from bitmap to byte array
public byte[] getBytesFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 70, stream);
return stream.toByteArray();
}
// get the base 64 string
String imgString = Base64.encodeToString(getBytesFromBitmap(someImg),
Base64.NO_WRAP);
回答by Hardik Nadiyapara
try below code
试试下面的代码
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte b [] = baos.toByteArray();
String base64String = Base64.encodeToString(b, Base64.DEFAULT);
Dont forget to import android.util.Base64;
不要忘记 import android.util.Base64;
回答by Shelm
maybe you can try
也许你可以试试
String base64Result = Base64.encodeToString(yourByteArray[], Base64.DEFAULT);
String base64Result = Base64.encodeToString(yourByteArray[], Base64.DEFAULT);
回答by Andrey
Method:
方法:
private String bitmapToBase64(Bitmap bitmap) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream .toByteArray();
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
Imports:
进口:
import android.util.Base64;
import java.io.ByteArrayOutputStream;