【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 工具的路径正确。

相关推荐
lingran__几秒前
C++ STL 关联式容器万字详解:set、multiset、map、multimap|接口用法 + 易错点 + 力扣真题实战
开发语言·c++·stl·set·map·红黑树·关联式容器
c238568 分钟前
C/C++每日一练21
c语言·开发语言·c++
qq_401700419 分钟前
Qt Modbus协议0x00
开发语言·qt
yushikong11 分钟前
关于rust开发ch32x033f8p6的一些记录
开发语言·后端·rust
wangjialelele15 分钟前
Spring Cloud 全体系学习笔记(REST、Nacos、Feign、Gateway)
java·spring boot·笔记·学习·spring·spring cloud·gateway
吴声子夜歌24 分钟前
正则表达式——环视
开发语言·正则表达式
weixin_BYSJ198726 分钟前
springboot医疗信息管理系统---附源码17465
java·javascript·spring boot·python·django·flask·php
xqqxqxxq33 分钟前
吃透 Spring 高频注解(包含SpringMVC Spring Boot)
java·spring boot·spring
都叫我大帅哥34 分钟前
Embabel 1.0 Agent Framework 完全指南:从入门到生产级实践
java
福大大架构师每日一题41 分钟前
agno v2.8.7 发布:顾问模型、精准路线、调度能力全面升级,10项关键修复一次看懂
java·开发语言·数据库