file_transfer/
file_transfer.rs

1use adb_kit::{ADB, transfer::TransferOptions, prelude::*};
2use std::path::Path;
3
4fn main() -> ADBResult<()> {
5    let adb = ADB::new(None);
6
7    // 列出设备
8    let devices = adb.list_devices()?;
9    if devices.is_empty() {
10        println!("没有连接的设备");
11        return Ok(());
12    }
13
14    let device_id = &devices[0].id;
15    println!("使用设备: {}", device_id);
16
17    // 推送文件
18    let local_file = "test_file.txt";
19    let device_path = "/sdcard/test_file.txt";
20
21    // 创建测试文件
22    if !Path::new(local_file).exists() {
23        std::fs::write(local_file, "这是一个测试文件内容")?;
24    }
25
26    // 定义传输选项
27    let options = TransferOptions {
28        ..Default::default()
29    };
30
31    // 推送文件
32    println!("推送文件: {} -> {}", local_file, device_path);
33    adb.push(device_id, local_file, device_path, Some(options.clone()))?;
34
35    // 检查文件是否存在
36    let exists = adb.file_exists(device_id, device_path)?;
37    println!("设备上文件存在: {}", exists);
38
39    // 获取文件大小
40    if exists {
41        let size = adb.get_file_size(device_id, device_path)?;
42        println!("文件大小: {} 字节", size);
43    }
44
45    // 拉取文件
46    let local_output = "downloaded_test_file.txt";
47    println!("拉取文件: {} -> {}", device_path, local_output);
48    adb.pull(device_id, device_path, local_output, Some(options))?;
49
50    // 删除设备上的文件
51    println!("删除设备上的文件: {}", device_path);
52    adb.remove_path(device_id, device_path, false)?;
53
54    Ok(())
55}