使用Python浏览Android设备目录

1 投票
1 回答
2728 浏览
提问于 2025-04-28 12:35

我希望用户在点击“浏览”按钮时能够查看和浏览安卓设备的文件夹,这样他们就可以选择存放照片的文件夹。

现在,我可以进入“adb shell”,但是在通过adb shell连接到设备后,我无法输入其他命令来进行操作。我想在通过adb shell连接到设备后,能够使用cd命令或其他命令。我的代码写得很糟糕,因为我不太明白“stdout=subprocess.PIPE”是什么意思,只是在跟着网上的教程。

这是我现在的代码:

from subprocess import check_output
import subprocess

out = check_output("adb shell ls")

print out

destination = raw_input("Choose a folder: ")

p = subprocess.Popen("adb shell",stdout=subprocess.PIPE)
out, err = p.communicate()

g = subprocess.call(['cd', destination], stdout=subprocess.PIPE)
out, err = g.communicate()

print out

我非常感谢任何帮助和指导。提前谢谢你们。

暂无标签

1 个回答

0

我建议你使用Android提供的功能来遍历文件夹和文件。比如说,你可以创建一个新文件,当然如果你用Java来做,这可能会更简单:

File currentDir = new File("/"); // "/" stands for root directory
File[] files = currentDir.listFiles(); // lists all files in the current directory, store them in the *files* array 
//you can supply as an argument a String[] array, with the file extensions if needed 
//(to show only .jpeg, .png, or only documents like .docx, .pdf)
//you can use Collections and Comparator classes to sort them
if(files != null && files.length > 0) { //check if it holds any files
        for(File f : files) {
            if(f.isHidden()) { 
                // don't add the hidden file to the list, or at your choice
                continue;
            } else {
            // add the file to the list
            fileList.add(f);
            }
        }
        Collections.sort(fileList, new FileComparator()); //
}
//you can also check if a specific *file* is file or directory with
file.isFile();
file.isDirectory();

你可以利用这些关于文件的信息,把它们显示在一个列表视图(ListView)或者网格视图(GridView)中。当你点击某个特定的项目时,可以更新currentDir,然后刷新内容或者打开文件等等。

这里有一个文件选择器的例子,链接在这里:http://www.dreamincode.net/forums/topic/190013-creating-simple-file-chooser/

撰写回答