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

相关推荐
乌鸦乌鸦你的小虎牙3 小时前
qt 5.12.8 配置报错(交叉编译环境)
开发语言·数据库·qt
无心水3 小时前
【OpenClaw:实战部署】5、全平台部署OpenClaw(Win/Mac/Linux/云服务器)——10分钟跑通第一个本地AI智能体
java·人工智能·ai·智能体·ai智能体·ai架构·openclaw
feifeigo1233 小时前
Leslie人口模型MATLAB实现(中长期人口预测)
开发语言·matlab
写代码的二次猿3 小时前
安装openfold(顺利解决版)
开发语言·python·深度学习
一只大袋鼠3 小时前
Redis 安装+基于短信验证码登录功能的完整实现
java·开发语言·数据库·redis·缓存·学习笔记
※DX3906※4 小时前
Java排序算法--全面详解面试中涉及的排序
java·开发语言·数据结构·面试·排序算法
笨笨马甲5 小时前
Qt QSS使用指南
开发语言·qt
cur1es5 小时前
【JVM类加载&双亲委派模型&垃圾回收机制】
java·jvm·gc·垃圾回收·类加载·双亲委派模型
Mr.朱鹏6 小时前
JVM-GC垃圾回收案例
java·jvm·spring boot·算法·spring·spring cloud·java-ee
焦糖玛奇朵婷6 小时前
实测扭蛋机小程序:开发简单,互动有趣
java·大数据·程序人生·小程序·软件需求