# How do i open a text file from a terminal window from within the android gui

I'm trying to open a text file from within a terminal window emulator -- but not so it opens in the actual terminal but more like I've just clicked on it.

So my question is: What command can I use to open /mnt/sdcard/textfile.txt from a terminal window emulator from ROM Toolbox Lite within the Android GUI so that it will open with any one of my text editing applications? Will I need to run su before I can run what ever command? I'd prefer not to, but the device is rooted so I can run su.

Im using Android version 4.0.4

• Do you want to open it in a seperate program? Or do you just want to quickely edit it? nano or vi are usually available on *nix based systems. I prefer nano because it is a bit cleaner imho. – John Apr 20 '13 at 23:40
• No i want the command to open the file with droid edit or with es text editor – Richard Lalaz Apr 21 '13 at 0:26
• Thank you for your input but i am looking for something simpler than that... I realized last night all i want to know is how to do this like in windows if you type in C:\text.txt from command prompt it will load the default text editor (what ever that may be) so how do i do it using bash terminal emulator from ROM Toolbox Lite? – Richard Lalaz Apr 21 '13 at 19:48
• You can always just rip the file off of the phone using adb and then open it on your computer – John Apr 22 '13 at 12:05

Launching apps in Android isn't straightforward. You'll have to use am start combined with certain <PARAMS> to launch the app or a particular section of it.

From the comments, it seems that you may want to launch the text file using ES Note Editor. In that case, first identify the activity responsible for the edit window you see when the file is opened in the editor using GUI.

Note: Tested the solution on Android 4.2, 4.4 and 5.0.

## Instructions

1. Open the file manually in the editor using GUI, and make sure that it stays active on the screen.
2. Connect your phone into PC in debugging mode, setup ADB on PC, and enter into PC's terminal/cmd the commands:

adb shell
su
dumpsys activity | grep mFocusedActivity


The output may look like:

mFocusedActivity: ActivityRecord{42839028 u0 com.estrongs.android.pop/.app.editor.PopNoteEditor}


The relevant info (activity) is .app.editor.PopNoteEditor and package name is com.estrongs.android.pop. You may use the command dumpsys activity to see some more useful stuff.

That's just for brief understanding. Keep the whole string (com.estrongs.android.pop/.app.editor.PopNoteEditor) ready to be used.

3. You may close the editor on Android. Open your favorite terminal app in Android and enter:

su
am start -n <PACKAGE_NAME/ACTIVITY> -d <FILEPATH> --activity-clear-task

• -n will allow to launch the app's mentioned component.
• <PACKAGE_NAME/ACTIVITY> is the string we noted in step 2. For ES Note Editor it would be com.estrongs.android.pop/.app.editor.PopNoteEditor.
• -d is to allow data to be passed to the app's activity.
• <FILEPATH> is the location of the text file you want to open.
• --activity-clear-task will make sure that the activity is not launched from "Recents" but from scratch.

(Enter am start to dive deeper into interesting stuff.)

In my case, the final query for file aero.txt under /sdcard/airdroid/ looked like:

am start -n com.estrongs.android.pop/.app.editor.PopNoteEditor -d file:///sdcard/airdroid/aero.txt --activity-clear-task


The file opened successfully.

4. I use QuickEdit Text Editor as default text editor and the relevant query looked like:

am start -n com.rhmsoft.edit/.activity.MainActivity -d file:///sdcard/airdroid/nmon.txt --activity-clear-task


Have fun!

• When you "Connect your phone into PC in debugging mode, setup ADB on PC", do you actually send commands to the phone via PC? – codezombie Nov 8 '15 at 18:28
• Yes. That's true. – Firelord Nov 9 '15 at 0:21

In addition to this answer you can use the following solution which is taken from rohitverma's answer here and noticed in Lucky's comment.

1. am start -a android.intent.action.VIEW -d <FILEPATH> -t text/plain --activity-clear-task


It will execute com.android.internal.app.ResolverActivity activity of android system with all candidates registered for "plain text" available for selection in a dialog.

For my file, the query was and dialog shown were:

am start -a android.intent.action.VIEW -d file:///sdcard/airdroid/dd.sh -t text/plain --activity-clear-task


.

1. You may use input keyevent <KEYCODE/KEYNAME> command to select the app in the dialog. Input events list is available here by LionCoder.

Relevant keyevents are:

19 -->  "KEYCODE_DPAD_UP"


And the usage would be like:

input keyevent 20


or

input keyevent KEYCODE_DPAD_DOWN


Use the combination of them to achieve the goal, and use 66 --> "KEYCODE_ENTER to simulate "enter".

I would still go for this answer if needed. :)