Android中Base64解码报bad base-64错误求助
Hey there, let's break down why you're hitting that java.lang.IllegalArgumentException: bad base-64 error when trying to load your base64 string into an ImageView. I've dealt with this exact issue a bunch of times, so here are the most common fixes to try:
Common Causes & Solutions
1. Your Base64 string has invalid characters or missing padding
Base64 strings can only contain specific characters: A-Z, a-z, 0-9, plus +// (for DEFAULT mode) or -/_ (for URL_SAFE mode), and optional = padding at the end. Any extra stuff like spaces, newlines, or random symbols will break decoding. Also, valid Base64 strings must be a multiple of 4 in length—if yours isn't, you need to add padding characters (=) to fix it.
Fix steps:
- First, clean your input string to strip out invalid characters:
// Use this regex for URL_SAFE mode; swap [-_] with [+/] if using DEFAULT String cleanedValue = value.replaceAll("[^A-Za-z0-9-_=]", ""); - Then, fix the padding if needed:
int paddingNeeded = 4 - (cleanedValue.length() % 4); if (paddingNeeded != 4) { cleanedValue += "=".repeat(paddingNeeded); } - Use this cleaned string instead of the original
valuefor decoding.
2. You didn't remove the "data URI" prefix
If your base64 string starts with something like data:image/png;base64,, that prefix isn't part of the actual base64 data—you have to strip it before decoding. This is super common when pulling base64 images from APIs or web sources.
Fix code:
if (value.startsWith("data:image/")) { // Split at the comma to get just the raw base64 part value = value.split(",")[1]; }
3. Mismatched encoding/decoding modes + hidden newlines
Even though you tried URL_SAFE, double-check that the string was encoded with the matching mode. For example, a DEFAULT-encoded string uses +//—decoding it with URL_SAFE will fail because -/_ aren't present. Also, some encoding methods add newline characters, which can break decoding.
Pro tip: Add Base64.NO_WRAP to your decoding flags to ignore any newline characters:
byte[] decodedString = Base64.decode(cleanedValue, Base64.URL_SAFE | Base64.NO_WRAP);
Updated Full Code Example
Putting all these fixes together, your code might look like this:
// Step 1: Strip data URI prefix if present if (value.startsWith("data:image/")) { value = value.split(",")[1]; } // Step 2: Clean invalid characters and fix padding String cleanedValue = value.replaceAll("[^A-Za-z0-9-_=]", ""); int paddingNeeded = 4 - (cleanedValue.length() % 4); if (paddingNeeded != 4) { cleanedValue += "=".repeat(paddingNeeded); } // Step 3: Decode and load the bitmap safely try { byte[] decodedString = Base64.decode(cleanedValue, Base64.URL_SAFE | Base64.NO_WRAP); Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); if (bitmap != null) { dashboard_img.setImageBitmap(bitmap); dashboard_img.invalidate(); } else { Log.e("ImageLoad", "Failed to create bitmap from decoded data (corrupted?)"); } } catch (IllegalArgumentException e) { Log.e("ImageLoad", "Base64 decoding failed", e); }
Give these steps a go—nine times out of ten, it's one of these small issues causing the error.
内容的提问来源于stack exchange,提问作者Faster

