简体   繁体   中英

Android: How to check if string is Base64 encoded?

I am not sure how to check if a String is Base64 encoded or not using android.Base64 . I know in standard Java using java.util.Base64 calling Base64.getDecoder().decode() would through an IllegalArgumentException if the String wasn't already in Base64, but on android I get no such error, so how can I check if it is Base64 encoded?

Add to Gradle:

implementation 'commons-codec:commons-codec:1.9'

Add to your code:

if (Base64.isBase64(yourString)) {
   //do stuff
} else {
   //do other stuff
}

try this:

   try{
     Base64.encode("thestringyouwanttocheck".getBytes(),Base64.DEFAULT);
   }catch(IllegalArgumentException e){
      Toast toast=Toast.makeText(context,"Illigal Input",duration);
      toast.show();
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM