Android java.lang.IllegalArgumentException: 错误的 base-64
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25217515/
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
java.lang.IllegalArgumentException: bad base-64
提问by Alexandru Patriche
I am trying to encode and decode a String using android.util.Base64, but it gives a bad base-64 error.
我正在尝试使用 android.util.Base64 对字符串进行编码和解码,但它给出了一个糟糕的 base-64 错误。
The code with the problem is:
有问题的代码是:
private byte[] base64ToByte(String str) throws IOException {
Log.i("encription", str);
byte[] returnbyteArray = Base64.decode(str, Base64.URL_SAFE);
return returnbyteArray;
}
The error logcat is:
错误日志是:
08-09 13:02:18.589: E/AndroidRuntime(29827): Process: com.example.maptest, PID: 29827
08-09 13:02:18.589: E/AndroidRuntime(29827): java.lang.IllegalArgumentException: bad base-64
08-09 13:02:18.589: E/AndroidRuntime(29827): at android.util.Base64.decode(Base64.java:161)
08-09 13:02:18.589: E/AndroidRuntime(29827): at android.util.Base64.decode(Base64.java:136)
08-09 13:02:18.589: E/AndroidRuntime(29827): at android.util.Base64.decode(Base64.java:118)
08-09 13:02:18.589: E/AndroidRuntime(29827): at com.example.maptest.security.Encription.base64ToByte(Encription.java:116)
08-09 13:02:18.589: E/AndroidRuntime(29827): at com.example.maptest.security.Encription.encode(Encription.java:103)
08-09 13:02:18.589: E/AndroidRuntime(29827): at android.os.Handler.dispatchMessage(Handler.java:102)
08-09 13:02:18.589: E/AndroidRuntime(29827): at android.os.Looper.loop(Looper.java:136)
08-09 13:02:18.589: E/AndroidRuntime(29827): at android.app.ActivityThread.main(ActivityThread.java:5081)
08-09 13:02:18.589: E/AndroidRuntime(29827): at java.lang.reflect.Method.invokeNative(Native Method)
08-09 13:02:18.589: E/AndroidRuntime(29827): at java.lang.reflect.Method.invoke(Method.java:515)
08-09 13:02:18.589: E/AndroidRuntime(29827): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
08-09 13:02:18.589: E/AndroidRuntime(29827): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-09 13:02:18.589: E/AndroidRuntime(29827): at dalvik.system.NativeStart.main(Native Method)
The input(the str String from the code) looks like this:
输入(代码中的 str 字符串)如下所示:
08-09 13:02:18.539: I/encription(29827): 26.919047981500626
It is a double converted to a string, using:
它是一个双精度转换为字符串,使用:
String.valueOf(number)
The error is persistent with all encoding flags (DEFAULT, NO_WRAP, etc), any help would be apreciated, thank you.
该错误对于所有编码标志(DEFAULT、NO_WRAP 等)都是持续存在的,任何帮助将不胜感激,谢谢。
回答by Mahm00d
You are trying to decode26.919047981500626
which you can't. Because it's nota valid base64 encoded string.
您正在尝试解码26.919047981500626
您无法解码的内容。因为它不是有效的 base64 编码字符串。
When put into this online base64 decoder, it gives this error:
当放入这个在线 base64 解码器时,它给出了这个错误:
The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.
输入不是有效的 Base-64 字符串,因为它包含非 Base-64 字符、两个以上的填充字符或填充字符中的非法字符。
Update:
更新:
If you want to know the valid formatting of a encoded base64 string, take a look at the table in the wikipedia articleand also this answerwhich shows a base64 validator code in C#.
如果您想知道已编码的 base64 字符串的有效格式,请查看维基百科文章中的表格以及此答案,其中显示了 C# 中的 base64 验证器代码。
回答by edo
remove the prefix string " data:image/png;base64, ", get the string of after "data:image/png;base64,", it can decode
去掉前缀字符串"data:image/png;base64,",得到"data:image/png;base64,"之后的字符串,就可以解码了
回答by Abhijeet
It hit me, if in any case your are splitting the data & the encoding it, then decode it first before putting it back together.. Stupidly I was doing decode after collation of data that left me "bad-base-64" as each block of encode data had file enders(==).
它击中了我,如果在任何情况下你正在拆分数据和编码它,然后在将它重新组合在一起之前先解码它。编码数据块具有文件结尾(==)。
This is Linkis handy anyways to validate the encoding and decoding.
这是Link无论如何都可以方便地验证编码和解码。