使用Kotlin for Android应用程序将图像编码为Base64

时间:2019-10-10 20:45:32

标签: android android-studio kotlin

(Not duplicate / I did research)

  • Kotlin使用Android
  • min API level19

我正在尝试将encode文件image放入Base64

我的图像路径如下:

/storage/emulated/0/Android/data/com.example.android/files/Pictures/1234.jpg

我将路径保存到名为photo_path

的变量中

现在,我将图像转换为Base64

val bytes = File(photo_path).readBytes()
var imgBase64 = android.util.Base64.encode(bytes, android.util.Base64.DEFAULT);

println("imgBase64: " + imgBase64)

当我在输出中打印imgBase64的值时,我得到了:

imgBase64: [B@4202e71

任何人都可以帮助我了解输出内容以及如何解决此问题吗?

1 个答案:

答案 0 :(得分:2)

如果要输出为String,则必须使用encodeToString方法

val imgBase64 = android.util.Base64.encodeToString(bytes, android.util.Base64.DEFAULT)