1

I would like my user to be able to view & navigate the directories of an Android device when they click the "Browse" button, so they will be able to choose the folder where photos are stored.

As of now, I can go into the "adb shell", but I am not able to enter any more commands to interact while connected to the device via adb shell. I would like to connect to device via adb shell and cd or run other commands after connecting to the device. My current code is really horrible as I do not understand what does "stdout=subprocess.PIPE" mean and I am just following some online tutorials.

Here is my current code:

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

I appreciate any help and guides given. Thank you in advance.

1

1 Answer 1

0

I'll suggest you to use the Android functions to iterate the directories and files. For example you create a new file, of course if you are making it in Java (and might be easier):

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();

You can use all this info about files to show them in a ListView or GridView, on a click on a specific item, you can update the currentDir and refresh the contents or open the file, etc.

Here is an example of a file chooser http://www.dreamincode.net/forums/topic/190013-creating-simple-file-chooser/

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for your prompt reply. I am writing my code in Python and I will appreciate it if you are able to guide me along the lines of Python's syntax (I will try to write it in Python myself in the meantime). Do you know if it is possible to use adb to connect to the adb shell and input more commands while in "adb shell" mode? Thank you so much.
How do you write a python app for Android? Do you use a framework, library? If yes, it should have already some functions. I found this one kyvi.org, for some additional features you can connect to it some modules kivy.org/docs/guide/android.html#plyer and it appears to have an example for creating a file chooser kivy.org/docs/api-kivy.uix.filechooser.html (using framework's functions).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.