Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: ANR when call getExternalFilesDir(null)

Recently I've started to get ANR errors when call getExternalFilesDir(null) from my activity code. This code was working earlier but now, maybe after updating gradle build tools, there is such problem. Cleaning the project and rebuild don't help. Did anyone get this problem also? Thanks in advance.

The sample of code. The app freezes with ANR dialog on the first line File folder = getExternalFilesDir(null);

    // check if downloaded files take less memory than EPISODES_SIZE_WARNING_LIMIT_MB
private void checkFolderSize() {
    File folder = getExternalFilesDir(null);
    if (folder != null) {
        long length = 0;
        for (File file : folder.listFiles())
            if (file.isFile())
                length += file.length();
        double sizeMb = ((double)length) / 1024 / 1024;
        if (sizeMb > EPISODES_SIZE_WARNING_LIMIT_MB) {
            new AlertDialog.Builder(this)
                    .setTitle(R.string.dialog_warning)
                    .setMessage(getString(R.string.warning_used_memory, EPISODES_SIZE_WARNING_LIMIT_MB))
                    .setNeutralButton(R.string.dialog_ok, null)
                    .show();
        }
    }
}
like image 336
Victor Semenovich Avatar asked Jul 04 '26 03:07

Victor Semenovich


1 Answers

That was device problem. After reboot it works.

like image 66
Victor Semenovich Avatar answered Jul 05 '26 17:07

Victor Semenovich