我想将base64中的webservice中的图像解码为位图,并在我的android应用程序中使用它。这是我的方法:
public Bitmap getCaptcha() throws IOException
{
List<NameValuePair> params = new ArrayList<NameValuePair>();
String json = jsonParser.getCaptcha(captchaURL, params);
Log.i("", json);
byte [] encodeByte=Base64.decode(json.getBytes(),Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
}
如果我用我的 php 脚本解码从 getCaptcha 方法获得的字符串 - 它会正确显示。但是当我在我的应用程序中这样做时,我得到一个
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.assignmentexpert/com.assignmentexpert.RegisterActivity}: java.lang.IllegalArgumentException: bad base-64
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalArgumentException: bad base-64
at android.util.Base64.decode(Base64.java:161)
at android.util.Base64.decode(Base64.java:136)
at com.library.UserFunctions.getCaptcha(UserFunctions.java:97)
at com.assignmentexpert.RegisterActivity.onCreate(RegisterActivity.java:66)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
... 11 more
它的原因是什么?
我添加了我得到的 base64 代码:
/9j/4AAQSkZJRgABAQAAAQABAAD//gA7Q1JFQVRPUjogZ2QtanBlZyB2MS4wICh1c2luZyBJSkcgSlBFRyB2NjIpLCBxdWFsaXR5ID0gMTUK/9sAQwA1JSgvKCE1LysvPDk1P1CFV1BJSVCjdXthhcGqy8i+qrq31fD//9Xi/+a3uv///////////87//////////////9sAQwE5PDxQRlCdV1ed/9y63P///////////////////////////////////////////////////////////////////8AAEQgAKAB4AwEiAAIRAQMRAf/EAB8AAAEFAQEBAQEBAAAAAAAAAAABAgMEBQYHCAkKC//EALUQAAIBAwMCBAMFBQQEAAABfQECAwAEEQUSITFBBhNRYQcicRQygZGhCCNCscEVUtHwJDNicoIJChYXGBkaJSYnKCkqNDU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6g4SFhoeIiYqSk5SVlpeYmZqio6Slpqeoqaqys7S1tre4ubrCw8TFxsfIycrS09TV1tfY2drh4uPk5ebn6Onq8fLz9PX29/j5+v/EAB8BAAMBAQEBAQEBAQEAAAAAAAABAgMEBQYHCAkKC//EALURAAIBAgQEAwQHBQQEAAECdwABAgMRBAUhMQYSQVEHYXETIjKBCBRCkaGxwQkjM1LwFWJy0QoWJDThJfEXGBkaJicoKSo1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5eoKDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uLj5OXm5+jp6vLz9PX29/j5+v/aAAwDAQACEQMRAD8A0mOBkUtI/T8R/OgjPcj6UADOF6nn0qjd3jqdkfynuamnfYRFCMyt39BVS8RYI0jHLH5mPrTSAmtJJHjJZyTnvVpSyjoGHqKhsRtts5wSan4bkA59qTDQY9yquqbW3N0BGKJJjEhd1+UehqnJl5VmJJHmBB9K0Nif3R+VFgsQC/hPcj6inrdRv93mla3ibqo/CqF7bpBtZCRntTCxo7nPRMfU1FPL5SFmfJ7KKisCZIzvyQOmaNQ4aEdF3f4UrBYWO6lVlFwm0N0PpVyorlQ9tID/AHSaLZ99ujHk45oAlooooAR/u/TmmTyiGMseT0A9TT3+430qO4hWZAG6A54oAii2wjdKw81+SSRxVG8ZXnJUgjHUHNX1sogP9WCfUk1UvoREylVABHamhlm0liESqWXdT7yXy4vlPztwKgtYYWiV2Q7s9RU1xA85R0YKV6ZFLqIhnTyraFSOjjn86vKyuMqQR7VnzwzfuxNLu3OBgVacx28XyjhewPNDAZJFchmaKbIJztPaqbcSf6VvzWjHOkg+Vwahvnj8kqcbu1NMCa3eJo8RYwO1Nu4PPjwDhgciqenBvNJGcYrQkkESF26Ck9GBTZ7oxGJo+oxv9qnsf+PRPx/nTJ72PyWCHLEYAqa2Qx26KeuOaYEtFFFIAIyCD0NJjLD0FFFAC0yVUcAOob0BoooAI4ljGFAFOKgnPf2oooAY0KuylsnacjJp5UEYIGKKKAIHs4X527T7UwafEDyWNFFFwLMcaRLtQYFKyhhhgCPQ0UUAMFvEGyI1z9KkwKKKACiiigD/2Q==n
问题是当我从浏览器复制代码并将其硬编码到我的程序中时 - 一切正常。但是当我通过我的 android 程序获取 base64 数据并尝试对 base64 进行硬编码时 - 我得到了一个异常。浏览器中的代码与我通过 Android 获得的代码看起来相同。但是,实际上,这并不是因为结果不同)