【Java】读取手机文件名称

首先,确保你已经连接了你的手机并已启用 USB 调试模式。然后,你需要使用 Android Debug Bridge(ADB)工具来获取手机文件列表。以下是一个简单的 Java 代码片段,使用 ProcessBuilder 调用 ADB 命令来获取文件列表:

java 复制代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class ADBFileLister {

    public static void main(String[] args) {
        String adbPath = "path/to/adb";  // 替换为你的 adb 路径
        String deviceID = "your_device_id";  // 如果只连接了一个设备,可以为 null

        // Android 文件路径
        String androidPath = "/storage/emulated/0/BaiduNetdisk/我的学习";

        List<String> fileList = listFiles(adbPath, deviceID, androidPath);

        // 打印文件列表
        for (String file : fileList) {
            System.out.println(file);
        }
    }

    private static List<String> listFiles(String adbPath, String deviceID, String androidPath) {
        List<String> fileList = new ArrayList<>();

        // 构建 ADB 命令
        List<String> command = new ArrayList<>();
        command.add(adbPath);
        if (deviceID != null) {
            command.add("-s");
            command.add(deviceID);
        }
        command.add("shell");
        command.add("ls");
        command.add(androidPath);

        // 执行 ADB 命令
        try {
            ProcessBuilder processBuilder = new ProcessBuilder(command);
            Process process = processBuilder.start();

            // 读取命令输出
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                fileList.add(line);
            }

            process.waitFor();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }

        return fileList;
    }
}

请注意

替换 adbPath 为你本地的 ADB 工具的路径,

deviceID 为你设备的 ID(可以通过 adb devices 查看),

androidPath 为你想要列出文件的 Android 设备路径。

此代码片段调用 ADB 命令 adb shell ls,并将输出的文件列表读取到 fileList 中。请确保你的手机已连接,并且 ADB 工具的路径正确。

相关推荐
2201_757830873 小时前
全局异常处理器
java
知远同学4 小时前
Anaconda的安装使用(为python管理虚拟环境)
开发语言·python
小徐Chao努力4 小时前
【Langchain4j-Java AI开发】09-Agent智能体工作流
java·开发语言·人工智能
CoderCodingNo4 小时前
【GESP】C++五级真题(贪心和剪枝思想) luogu-B3930 [GESP202312 五级] 烹饪问题
开发语言·c++·剪枝
Coder_Boy_4 小时前
SpringAI与LangChain4j的智能应用-(理论篇3)
java·人工智能·spring boot·langchain
kylezhao20195 小时前
第1章:第一节 开发环境搭建(工控场景最优配置)
开发语言·c#
啃火龙果的兔子5 小时前
JavaScript 中的 Symbol 特性详解
开发语言·javascript·ecmascript
Coder_Boy_5 小时前
基于SpringAI的智能平台基座开发-(六)
java·数据库·人工智能·spring·langchain·langchain4j
热爱专研AI的学妹5 小时前
数眼搜索API与博查技术特性深度对比:实时性与数据完整性的核心差异
大数据·开发语言·数据库·人工智能·python
Mr_Chenph5 小时前
Miniconda3在Windows11上和本地Python共生
开发语言·python·miniconda3