Android Bitmap到Base64字符串(Android Bitmap to Base64 String)

如何将一个大的Bitmap(用手机相机拍摄的照片)转换为Base64 String?

How do I convert a large Bitmap (photo taken with the phone's camera) to a Base64 String?

原文:https://stackoverflow.com/questions/9224056

更新时间:2019-11-21 18:31

最满意答案

使用以下方法将位图转换为字节数组:

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);

byte[] byteArray = byteArrayOutputStream .toByteArray();

从字节数组中使用以下方法对base64进行编码

String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);

use following method to convert bitmap to byte array:

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);

byte[] byteArray = byteArrayOutputStream .toByteArray();

to encode base64 from byte array use following method

String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);

2013-12-12

相关问答

假设您的图像数据位于一个名为myImageData的字符串中,以下内容应该可以做到这一点: byte[] imageAsBytes = Base64.decode(myImageData.getBytes(), Base64.DEFAULT);

ImageView image = (ImageView)this.findViewById(R.id.ImageView);

image.setImageBitmap(

BitmapFactory.deco

...

public static String encodeToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)

{

ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();

image.compress(compressFormat, quality, byteArrayOS);

return Base64.enc

...

使用以下方法将位图转换为字节数组: ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);

byte[] byteArray = byteArrayOutputStream .toByteArray();

从字节数组中使用以下方法对base64进行编码 St

...

您只需使用其他内置方法即可恢复代码。 byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);

Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

You can just basically revert your code using some other built in meth

...

我发现了问题。 问题在于logcat来自我复制编码字符串的地方。 Logcat没有显示整个String,因此破碎的字符串没有为我解码图像。 因此,当我将编码的字符串直接传递给解码函数时,图像是可见的。 I have found out the problem. The problem was with the logcat from where i was copying the encoded string. Logcat didn't display the entire String so

...

试试这个位图; public Bitmap convert(String img){

byte[] b = Base64.decode(img, Base64.DEFAULT);

return BitmapFactory.decodeByteArray(b, 0, b.length);

}

而这就是String public String convert(Bitmap bm, int quality){

ByteArrayOutputStream baos = n

...

表示图像的前三个base64字符串解码正常。 但是接下来的四个产生了一个bad base-64在线的捕获 byte[] decodedString = Base64.decode(photo, Base64.URL_SAFE);

之前我说过。 如果你愿意的话 byte[] decodedString = Base64.decode(photo, Base64.DEFAULT);

然后没有捕获。 所有7个base64字符串解码都可以。 The first three base64 strings

...

如果你已经在文件中有了它,那么就没有理由对它进行解压缩然后重新压缩 - 这可能是导致错误的原因,因为每次压缩都是有损的并且会导致数据进一步丢失。 如果您已有图像文件,请将其作为原始字节和Base64读取。 If you already have it in the file, there is no reason to decompress it then recompress it- which may be the cause of your errors, as each compressi

...

你可以使用httpmime库上传任何文件(图像,音频,视频等..)请参考下面的代码。 HttpClient httpClient = new DefaultHttpClient();

HttpContext localContext = new BasicHttpContext();

HttpPost postRequest = new HttpPost(your url);

MultipartEntity reqEntity = new MultipartEntity(

Htt

...

只需将base64字符串转换为位图,而不是使用下面的代码将该位图加载到imageview中 byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);

Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);

image.setImageBitmap(decodedByte);

Just con

...

android bitmap string,Android Bitmap到Base64字符串(Android Bitmap to Base64 String)相关推荐

  1. android 字符串转bitmap,android – 如何将Base64字符串转换为BitMap图像显示在ImageView?...

    我有一个Base64字符串,表示一个BitMap图像. 我需要将该字符串转换为BitMap图像,以使用它在ImageView在我的Android应用程序 怎么做? 这是我用来将图像转换为base64字 ...

  2. Base64工具类(文件、base64字符串、Bitmap相互转换)

    马上吃饭了,懒得写太多了,先对付看吧 /*** Author : 马占柱* E-mail : mazhanzhu_3351@163.com* Time : 2019/5/19 9:30* Desc : ...

  3. java base64转bitmap,如何将Bitmap位图与base64字符串相互转换

    先引用delphi自带的单元 uses EncdDecd; 然后就可以使用下面二个函数了: ///将Bitmap位图转化为base64字符串 function BitmapToString(img:T ...

  4. 【base64】java 通过图片的Base64字符串判断文件格式

    代码:// base64字符串转写为文件public static void convertBase64DataToImage(String base64ImgData, String filePat ...

  5. C++字符串的不同存放类型 (string/char[])

    C++中,有两种类型的字符串表示形式: C-风格字符串 C++引入的string类 C-风格字符串中有大量的函数用来操作以 null 结尾的字符串: 1    strcpy(s1,s2)    复制字 ...

  6. base64转图片+图片转base64

    base64转图片,方法一: /*** 1.base64转图片* @param base64str1 base64码* @param savePath 图片路径* @return*/ public s ...

  7. Java接受前端的base64,转换失败。base64转图片互相转换

    spring boot前后端vue,base64转图片互相转换 vue就不展示了,下边只展示Java中互相转换的例子. Java实现图片转化成base64字符串 //图片转化成base64字符串 pu ...

  8. Android kotlin 将Base64字符串转换成Bitmap,并在jetpack compose的Image控件中显示

    Android kotlin 将Base64字符串转换成Bitmap 前言 代码 将Base64字符串转换成Bitmap 在jetpack compose的Image控件中显示Bitmap 完事 前言 ...

  9. 【Android 内存优化】Android 工程中使用 libjpeg-turbo 压缩图片 ( JNI 传递 Bitmap | 获取位图信息 | 获取图像数据 | 图像数据过滤 | 释放资源 )

    文章目录 一.Bitmap 图像数据处理 二.Java 层 Bitmap 对象转为 JNI 层 bitmap 对象 三.获取 bitmap 中的图像数据 四.过滤 bitmap 中的图像数据 ( 获取 ...

最新文章

  1. Java RTTI运行时类型识别
  2. SDN你必须知道的十大问题——SDN有哪些开源项目
  3. 1:Hello world
  4. Node.js v7 Beta版引入citgm
  5. 牛客题霸 [矩阵的最小路径和] C++题解/答案
  6. 大数组情况下栈溢出解决
  7. 内核调试工具 — kdump crash
  8. linux ikev1切换到ikev2,IKEv2与IKEv1的差异.doc
  9. android studio真机测试怎么跳过确定,AndroidStudio真机测试
  10. 进阶学习,如何无代码设计一款美观且实用的网站?
  11. 如何对APP进行重新签名(使用命令签名)
  12. ACS 2017中国汽车CIO峰会10月强势登陆上海
  13. iphone照片恢复至android,将照片从Android传输到iPhone的8种方法很容易
  14. MySQL修改表的字段
  15. 内存卡计算机管理员权限获得,U盘内存卡批量只读加密专家如何修改管理员密码...
  16. Spring学习笔记(六)。Spring自动装配,按名称自动装配:byName,按类型自动装配:byType,@Autowired,@Qualifier,@Resource
  17. 开源夏令营《基于HackRF开发GPS信号仿真模拟器》终期汇报
  18. opencv 视频中人脸检测
  19. sde无法连接oracle,SDE无法连接问题处理过程
  20. Java虚拟机的中的HotSpot技术,和JIM(Just In Time).

热门文章

  1. 记一次.net core 集成vue 实践
  2. java怎么获取该项目系统的参数_Java 获取系统参数
  3. [转]IPython介绍
  4. ArcGIS10.6中,在3D分析工具中创建视线之后,怎么将其删除?
  5. Android之安卓8.0版本以上手机开启热点提示Caller already has an active LocalOnlyHotspot request
  6. Android之中获取应用程序(包)的信息-----PackageManager的使用(一)
  7. sudo apt-get update
  8. python删除列表中的偶数_Python:从列表中删除奇数
  9. redis php方案,Redis三种部署方案图文详解
  10. 机器学习与数据挖掘——第二章 数据与数据预处理