开发者

Deep Search in the android file system for text files

I would like to search within the Android file system for finding text files containing specific information (e.g., the device's battery curr开发者_Go百科ent and voltage).

I tried to use the Astro File Browser but it seems that its search functionality does not support deep search (search within children directories as well).

I also tried to copy the directory in which I would like to search (the /sys/ directory) to my PC's hard drive using ADB but unfortunately this doesn't work as most of the files aren't copied due to missing reading access.

Do you no any working solution to browse the file system (either from a PC or via an Android app)?


Each manufacturer puts files in different places (or at least can). Try using the API to access this information:

There is an intent, ACTION_BATTERY_CHANGED

And the BatteryManager class

As for browsing the file system, Astro works fine - but if the OS is not granting you permissions, you will not be able to read it unless you root your phone.


I now implemented a simple, stupid Java method that can be executed from the Eclipse ADT to search in the file system and it works (however, slowly):

public static List<File> getFilesOfDirectory(File dir, String fileNamePart) {

    List<File> result;

    if (dir.exists() && dir.isDirectory()
            && !dir.getAbsolutePath().contains("root")
            && dir.getAbsolutePath().split("/").length < 10) {
        Log.i("fileSearch", "search in directory " + dir.getAbsolutePath());
        File[] files = dir.listFiles();

        if (files != null)
            result = new ArrayList<File>(Arrays.asList(files));
        else
            result = new ArrayList<File>();
    }

    else
        result = Collections.emptyList();

    /* Filter for interesting files. */
    List<File> filteredResult = new ArrayList<File>();
    for (File file : result) {
        if (file.getAbsolutePath().toLowerCase()
                .contains(fileNamePart.toLowerCase()))
            filteredResult.add(file);
        // no else.
    }

    /* Search recursively. */
    List<File> furtherResults = new ArrayList<File>();
    for (File file : result) {
        if (file.isDirectory())
            furtherResults.addAll(getFilesOfDirectory(file, fileNamePart));
        // no else.
    }
    // end for.

    filteredResult.addAll(furtherResults);

    return filteredResult;
}

The method can be called using a file object representing the root directory to be searched (e.g., new File("/")) and a String representing file names to be found (e.g., "voltage").

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜