atozcodes
atozcodes

Reputation: 75

How to fix this java.lang.IllegalArgumentException: bad base-64

i am new android devloper How to fix this. I have tried everything and I'll make a mistake if you know I have to answer. java.lang.IllegalArgumentException: bad base-64 This bug is like this. bad base-64 I do not understand how to change this.Below I am pleased to give you a step by step understanding

public class Tools {

    public static void getTheme(Context context) {
        SharedPref sharedPref = new SharedPref(context);
        if (sharedPref.getIsDarkTheme()) {
            context.setTheme(R.style.AppDarkTheme);
        } else {
            context.setTheme(R.style.AppTheme);
        }
    }

    public static void getLayoutDirections(Activity activity, boolean directions) {
        if (directions) {
            activity.getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
        }
    }

    public static void notificationOpenHandler(Context context, Intent getIntent) {
        long unique_id = getIntent.getLongExtra("unique_id", 0);
        long post_id = getIntent.getLongExtra("post_id", 0);
        String title = getIntent.getStringExtra("title");
        String link = getIntent.getStringExtra("link");
        if (post_id == 0) {
            if (link != null && !link.equals("")) {
                //context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(link)));
                Intent intent = new Intent(context, ActivityWebView.class);
                intent.putExtra("title", title);
                intent.putExtra("url", link);
                context.startActivity(intent);
            }
        } else if (post_id > 0) {
            Intent intent = new Intent(context, ActivityNotificationDetail.class);
            intent.putExtra("id", String.valueOf(post_id));
            context.startActivity(intent);
        } else {
            Intent intent = new Intent(context, MainActivity.class);
            context.startActivity(intent);
        }
        Log.d("push_notification", "unique id : " + unique_id);
        Log.d("push_notification", "link : " + link);
        Log.d("push_notification", "post id : " + post_id);
    }

    public static String withSuffix(long count) {
        if (count < 1000) return "" + count;
        int exp = (int) (Math.log(count) / Math.log(1000));
        return String.format("%.1f%c", count / Math.pow(1000, exp), "KMGTPE".charAt(exp-1));
    }

    public static long timeStringtoMilis(String time) {
        long milis = 0;
        try {
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = sd.parse(time);
            milis = date.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return milis;
    }

    public static int getGridSpanCount(Activity activity) {
        Display display = activity.getWindowManager().getDefaultDisplay();
        DisplayMetrics displayMetrics = new DisplayMetrics();
        display.getMetrics(displayMetrics);
        float screenWidth = displayMetrics.widthPixels;
        float cellWidth = activity.getResources().getDimension(R.dimen.recycler_item_size);
        return Math.round(screenWidth / cellWidth);
    }

    public static int dpToPx(Context c, int dp) {
        Resources r = c.getResources();
        return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
    }

    public static boolean isNetworkAvailable(Activity activity) {
        ConnectivityManager connectivity = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
        Network[] networks = connectivity.getAllNetworks();
        NetworkInfo networkInfo;
        for (Network mNetwork : networks) {
            networkInfo = connectivity.getNetworkInfo(mNetwork);
            if (networkInfo.getState().equals(NetworkInfo.State.CONNECTED)) {
                return true;
            }
        }
        return false;
    }

    public static String decrypt(String code) {
        return decodeBase64(decodeBase64(code));
    }

    public static String decodeBase64(String code) {
        byte[] valueDecoded = Base64.decode(code.getBytes(StandardCharsets.UTF_8), Base64.DEFAULT);
        return new String(valueDecoded);
    }

    public static boolean isConnect(Context context) {
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            if (activeNetworkInfo != null) {
                return activeNetworkInfo.isConnected() || activeNetworkInfo.isConnectedOrConnecting();
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }

    public static String getJSONString(String url) {
        String jsonString = null;
        HttpURLConnection linkConnection = null;
        try {
            URL linkurl = new URL(url);
            linkConnection = (HttpURLConnection) linkurl.openConnection();
            int responseCode = linkConnection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                InputStream linkinStream = linkConnection.getInputStream();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int j = 0;
                while ((j = linkinStream.read()) != -1) {
                    baos.write(j);
                }
                byte[] data = baos.toByteArray();
                jsonString = new String(data);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (linkConnection != null) {
                linkConnection.disconnect();
            }
        }
        return jsonString;
    }

    public static String getFormatedDateSimple(String date_str) {
        if (date_str != null && !date_str.trim().equals("")) {
            SimpleDateFormat oldFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            SimpleDateFormat newFormat = new SimpleDateFormat("MMMM dd, yyyy");
            try {
                String newStr = newFormat.format(oldFormat.parse(date_str));
                return newStr;
            } catch (ParseException e) {
                return "";
            }
        } else {
            return "";
        }
    }

    public static void getCategoryPosition(Activity activity, Intent intent) {

        if (intent.hasExtra("category_position")) {
            String select = intent.getStringExtra("category_position");
            if (select != null) {
                if (select.equals("category_position")) {
                    if (activity instanceof MainActivity) {
                        ((MainActivity) activity).selectFragmentCategory();
                    }
                }
            }
        }

    }

    public static void getRecipesPosition(Activity activity, Intent intent) {

        if (intent.hasExtra("recipes_position")) {
            String select = intent.getStringExtra("recipes_position");
            if (select != null) {
                if (select.equals("recipes_position")) {
                    if (activity instanceof MainActivity) {
                        ((MainActivity) activity).selectFragmentRecipe();
                    }
                }
            }
        }

    }

}





2021-11-26 08:16:48.778 11368-11404/com.app.yourrecipesapp E/AwareLog: AtomicFileUtils: readFileLines file not exist: android.util.AtomicFile@9b1a18a
    2021-11-26 08:16:49.834 11368-11445/com.app.yourrecipesapp E/libc: Access denied finding property "qemu.hw.mainkeys"
    2021-11-26 08:16:49.835 11368-11445/com.app.yourrecipesapp E/libc: Access denied finding property "ro.serialno"
    2021-11-26 08:16:50.350 11368-11368/com.app.yourrecipesapp E/OneSignal: Waiting for remote params. Moving setSubscription() operation to a pending queue.
    2021-11-26 08:16:50.423 11368-11368/com.app.yourrecipesapp E/AndroidRuntime: FATAL EXCEPTION: main
        Process: com.app.yourrecipesapp, PID: 11368
        java.lang.RuntimeException: Unable to create application com.app.yourrecipesapp.activities.MyApplication: java.lang.IllegalArgumentException: bad base-64
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6654)
            at android.app.ActivityThread.-wrap2(Unknown Source:0)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2084)
            at android.os.Handler.dispatchMessage(Handler.java:109)
            at android.os.Looper.loop(Looper.java:166)
            at android.app.ActivityThread.main(ActivityThread.java:7555)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963)
         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.app.yourrecipesapp.utils.Tools.decodeBase64(Tools.java:125)
            at com.app.yourrecipesapp.utils.Tools.decrypt(Tools.java:121)
            at com.app.yourrecipesapp.activities.MyApplication.requestTopic(MyApplication.java:88)
            at com.app.yourrecipesapp.activities.MyApplication.initNotification(MyApplication.java:57)
            at com.app.yourrecipesapp.activities.MyApplication.onCreate(MyApplication.java:47)
            at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1123)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6636)
            at android.app.ActivityThread.-wrap2(Unknown Source:0) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2084) 
            at android.os.Handler.dispatchMessage(Handler.java:109) 
            at android.os.Looper.loop(Looper.java:166) 
            at android.app.ActivityThread.main(ActivityThread.java:7555) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:469) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:963) 

Upvotes: 1

Views: 1526

Answers (1)

REInVent
REInVent

Reputation: 123

I can see an error in your code, try fixing this and see what happens.

public static String decrypt(String code) {
//return decodeBase64(decodeBase64(code));
//Should be:
return decodeBase64(code);
}

Upvotes: 1

Related Questions