Android Studio Base64编、解码

目录

1.定义

2.函数

3.标志位(flags)

4.示例

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 StringencodeToString(byte[] input, int offset, int len, int flags)变量1的字节数组从offset位置开始编码len个长度并返回其编码后的字符串
static StringencodeToString(byte[] input, int flags)把给定的字节数组编码成字符串返回

3.标志位(flags)

标志位说明注释
CRLFEncoder flag bit to indicate lines should be terminated with a CRLF pair instead of just an LF.用于指示行应以CRLF对而不只是LF来终止
DEFAULTDefault values for encoder/decoder flags.编码器/解码器标志的默认值
NO_CLOSEFlag 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_PADDINGEncoder flag bit to omit the padding '=' characters at the end of the output (if any).用于省略输出末尾的填充字符'='
NO_WRAPEncoder flag bit to omit all line terminators (i.e., the output will be on one long line).用于省略所有行终止符(即输出将在一行上连续输出)
URL_SADEEncoder/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);
    }

  • 31
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

西瓜江

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值