目录
1.定义
Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法。
2.函数
类型 | 函数 | 注释 |
static byte[] | decode(String str, int flags) | 变量1提供一个字符串,并按照flags,返回一个已经解码的字节数组 |
static byte[] | decode(byte[] input, int flags) | 变量1提供一个字节数组,并按照flags,返回一个已经解码的字节数组 |
static byte[] | decode(byte[] input, int offset, int len, int flags) | 变量1提供一个字节数组,并按照flags标志,返回一个从第offset位置开始解码len个长度的字节数组 |
static byte[] | encode(byte[] input, int flags) | 变量1提供一个字节数组,并按照flags,返回一个已经编码的字节数组 |
static byte[] | encode(byte[] input, int offset, int len, int flags) | 变量1提供一个字节数组,并按照flags标志,返回一个从第offset位置开始编码码len个长度的字节数组 |
static String | encodeToString(byte[] input, int offset, int len, int flags) | 变量1的字节数组从offset位置开始编码len个长度并返回其编码后的字符串 |
static String | encodeToString(byte[] input, int flags) | 把给定的字节数组编码成字符串返回 |
3.标志位(flags)
标志位 | 说明 | 注释 |
CRLF | Encoder flag bit to indicate lines should be terminated with a CRLF pair instead of just an LF. | 用于指示行应以CRLF对而不只是LF来终止 |
DEFAULT | Default values for encoder/decoder flags. | 编码器/解码器标志的默认值 |
NO_CLOSE | Flag to pass to Base64OutputStream to indicate that it should not close the output stream it is wrapping when it itself is closed. | 传递给Base64OutputStream的标志,指示当Base64OutputStream自身关闭时,它不应关闭其封装的输出流。 |
NO_PADDING | Encoder flag bit to omit the padding '=' characters at the end of the output (if any). | 用于省略输出末尾的填充字符'=' |
NO_WRAP | Encoder flag bit to omit all line terminators (i.e., the output will be on one long line). | 用于省略所有行终止符(即输出将在一行上连续输出) |
URL_SADE | Encoder/decoder flag bit to indicate using the "URL and filename safe" variant of Base64 (see RFC 3548 section 4) where - and _ are used in place of + and / . | 指示使用Base64的“URL和文件名安全”变体,其中-和_用于替代+和/ |
4.示例
private void Base64()
{
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();//创建对应流对象
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);//将图片和与bitmap对应
byte [] ImageByte=byteArrayOutputStream.toByteArray();//使用流对象吗,将bitmap转为数组
//String ImageString = Base64.encodeToString(ImageByte,Base64.DEFAULT);//输出为String
byte [] ImageEnByte = Base64.encode(ImageByte,Base64.DEFAULT);//输出为字节数组
//解码
byte[] ImageDeByte = Base64.decode(ImageEnByte,Base64.DEFAULT);
Bitmap ImageShow = BitmapFactory.decodeByteArray(ImageDeByte, 0, ImageDeByte.length);
ImageView imageView = findViewById(R.id.ImageShow1);
imageView.setImageBitmap(ImageShow);
}