-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ability to use usb otg storage in termux #71
Comments
I am on Android 5 and I can write to /storage/usbdisk without getting a "permission denied" error. Can you write to the usbdisk with other apps? |
Yes, the behaviour changed in Android 6 a bit for external storage as far as i know. With the first Builds the drive could only be accessed from the storage settings, but starting with an update of es Explorer itmcould be opened directly as well. |
@fornwall Is there anything that can be done here? |
@Neo-Oli I'm not sure, need to test with a usb otg storage. Perhaps direct file system access is not possible, and an api method could be added instead. |
It seems that direct file system access is not possible starting from Android 6.0 - from Android USB media support documentation, as mentioned in this stackoverflow answer:
However, a command could be created ( Also, it should be possible to write a |
The |
My thumb drive is mounted in |
would this be of use: having access to a usb drive for backup or additional storage would be nice. |
I need access to the OTG usb drive. |
This should be fixable by requesting the new permission known as "all files access" or "manage_external_storage" available from Android 11. USB drives would be available under /mnt/media_rw/abcd-1234 |
Greetings, I've just tried-out my external SSD (NTFS formatted, one among total of two partitions, the other one is FAT16 ESP) manually linked to EDIT: my phone's not rooted, didn't use adb/wireless debugging for the purpose, nor do I have enabled 'adb security permissions' - as Xiaomi states it. |
@analoginterface0 On AOSP or analogs it is not possible to grant full write access to all files on external disks (SD or USB) except the stored under directory The suggestion made by @locuturus also not correct either. All files access permission doesn't work in such way, it only provides a way for application to opt-out from scoped storage (where it can access only given type, e.g. photos) in favor of full access. Such behavior is specific to Android 10 and higher. Xiaomi, Samsung and other manufacturers who customize Android may change permission behavior. I'm not saying about custom ROMs where permissions can be messed up even more. If something works for you, this doesn't mean same works for everyone. The storage behavior is well tested by Termux devs and so far there is no proper solution for this issue. Though we have set of Termux:API commands like |
@sylirre I don't think you have that quite right. The documentation clearly states that All Files Access provides raw file path to apps with that permission. It is not just an escape from Scoped Storage restrictions it actually for the first time in modern Android history grants real file path to any directory you can reach with SAF and that includes USB. AOSP and Pixels and the like work just fine. There is another app, Syncthing Fork, which cannot use SAF because the real work is done in Go and that cannot use the SAF API so there was no way to access USB drives without root. But now that the app has All Files Access you can manually give it file paths to any USB directory and it works. I'm a little confused by @analoginterface0 situation, it reads to me like MIUI 13 can grant All Files Access to apps below the API level normally required. But for the rest of us AOSP folks I stand by what I wrote. Test it for yourself with Syncthing Fork, use the web interface, determine the real path to the USB you're testing and type it in there. |
The |
Quoting https://developer.android.com/training/data-storage/manage-all-files
Note that it ONLY says "direct file path access" for internal storages. |
There are many features of the various file access methods not detailed in that paragraph. You can get file path from media store DATA column but that is not mentioned either. I would not read very far into every omission. Using TermOne Plus I demonstrate on Sony Xperia 5 iii Android 13 stock ROM how to discover and read and write USB attached storage. This is what MANAGE_EXTERNAL_STORAGE permission makes possible in AOSP: I understand the other factors keeping Termux on API 28 but I want to set the record straight -- All Files Access is what every dev has been wanting all these years: real file paths to user accessable storage (aside from *Android/data). |
how do you allow this MANAGE_EXTERNAL_STORAGE permission for termux? |
After further investigation for changes in Android
WRITE_EXTERNAL_STORAGE vs MANAGE_EXTERNAL_STORAGEEnabling However, granting Storage Permissions VisibilityAssuming
How does MANAGE_EXTERNAL_STORAGE grant unreliable/removable volume access?Access is granted based on DAC and not SeLinux itself despite the
If an app has been granted the App process groups can be checked with
|
@agnostic-apollo I installed the Termux from Github Actions build. "All files access" permission did finally appear for Termux and I granted it. But when I check for groups in Termux, I still don't have Android 11 |
|
Indeed. I installed an Android 14 ROM and after rooting, termux seems to have |
cool, although fuse passthrough would not apply to otg as that won't be emulated... |
On Android 12, I plug an OTG USB C dongIe formated exFat. With another app, having the same right MANAGE_EXTERNAL_STORAGE, this command works. It is with this app that I know that 8678-0073 is the identifier of my USB Key. |
I may have to rethink logic for requesting permission, unless there are other advantages of legacy storage. |
Thank you @agnostic-apollo , it works. |
And thanks for telling me in which file are manage permission.
|
Following should probably work, i haven't tested, check if it returns https://developer.android.com/reference/android/os/storage/StorageManager#getStorageVolumes() https://developer.android.com/reference/android/os/storage/StorageVolume#getDirectory() As for requesting permission, check following, should only call it for Android termux-app/termux-shared/src/main/java/com/termux/shared/android/PermissionUtils.java Line 342 in 8e3a898
|
Good idea mount for finding the path of USB storage, this works fine : |
Best use
We match the first two fields in the regex and then get the second one with However, like I said, this will still return more that one entry if both a usb drive and an external sd card is mounted. You will then have to find for which entry |
If I'm in the shell already I like Edit: works for removable sdcard too, and remember with hubs you can attach multiple USB drives so it'll be tricky to parse any output for exactly 1 drive reliably. |
Thanks a lot @agnostic-apollo , this command works perfectly, and it is surely more reliable than mine. You are rignt, on my device their are only number, but it can be A-Z letter also. I keep your command for my initialization script. It search for external storage (SD or USB), if it found one it search on it the configuration script with a @locuturus , on my Android 12 device, the USB storage SOMETIMES also appears in |
@agnostic-apollo , about the permission "Manage all files", I apologize because I have made something probably very dirty, but which makes what I need :
I tried to add this piece of code in requestStoragePermission() but in this case it was called twice, I didn't understand why. NB : the authorization require a restart of Termux-app to be taken into account. Is there a way to restart within the shell ? something like : kill $(ps -ef | grep 'com.termux' | grep -v 'grep' | grep -v 'com.termux.' |awk '{ print $2 }'); am start -n com.termux/.HomeActivity; exit |
Ok, good to know it's not reliable across devices and/or OS versions. A little strange though - I picked this for myself a while ago because it actually does work on an A13 Samsung Tab S6 tablet and I was having trouble with that device. |
You can use this function in You can paste the function in terminal or add to find_most_recent_unreliable_storage_mount() {
local return_value
local i
local storage_mounts_string
local storage_mount
local storage_mount_basename
local unreliable_storage_mount
local android_build_version_sdk
local valid_number_regex='^[0-9]+$'
local -a storage_mounts_array=()
# Find android sdk/os version
# Termux app exports ANDROID__BUILD_VERSION_SDK for `>= v0.119.0`
if [[ ! "$ANDROID__BUILD_VERSION_SDK" =~ $valid_number_regex ]]; then
android_build_version_sdk="$(unset LD_LIBRARY_PATH; unset LD_PRELOAD; getprop "ro.build.version.sdk")"
return_value=$?
if [ $return_value -ne 0 ] || [[ ! "$android_build_version_sdk" =~ $valid_number_regex ]]; then
echo "Failure while finding \"ro.build.version.sdk\" property" 1>&2
echo "ANDROID__BUILD_VERSION_SDK = \"$android_build_version_sdk\"" 1>&2
if [ $return_value -eq 0 ]; then
return_value=1
fi
return $return_value
fi
else
android_build_version_sdk="$ANDROID__BUILD_VERSION_SDK"
fi
# If on Android `< 11`
if [ "$android_build_version_sdk" -lt 30 ]; then
echo "Cannot find unreliable storage mounts on Android sdk version '< 30'. Current sdk version is '$android_build_version_sdk'." 1>&2
return 1
fi
storage_mounts_string="$(grep -E "^[^ ]+ /mnt/media_rw/[A-Z0-9]{4}-[A-Z0-9]{4} " /proc/mounts | cut -d' ' -f2)"
return_value=$?
if [ $return_value -ne 0 ]; then
echo "Failure while finding most recent unreliable storage mount" 1>&2
return $return_value
fi
if [ -z "$storage_mounts_string" ]; then
echo "Failed to find any reliable or unreliable storage mounts" 1>&2
return 1
fi
IFS=$'\n' read -r -d '' -a storage_mounts_array <<< "$storage_mounts_string"
for (( i=${#storage_mounts_array[@]} - 1; i >= 0; i-- )) ; do
storage_mount="${storage_mounts_array[i]}"
storage_mount_basename="${storage_mount##*/}"
grep -qE "^[^ ]+ /storage/$storage_mount_basename " /proc/mounts
return_value=$?
if [ $return_value -eq 1 ]; then
unreliable_storage_mount="$storage_mount"
break
elif [ $return_value -ne 0 ]; then
echo "Failure while finding if storage mount is unreliable" 1>&2
return $return_value
fi
done
if [ -z "$unreliable_storage_mount" ]; then
echo "Failed to find any unreliable storage mounts" 1>&2
return 1
fi
echo "$unreliable_storage_mount"
}
The the
|
You are calling if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
if (Environment.isExternalStorageManager())
return true;
errmsg = context.getString(R.string.msg_storage_permission_not_granted);
Logger.logError(LOG_TAG, errmsg);
if (showErrorMessage)
Logger.showToast(context, errmsg, false);
if (requestCode >= 0)
requestManageStorageExternalPermission(context, requestCode);
return false;
}
You can run
If you just want to kill from java, then can use following. Note the import, import android.os.Process;
try {
finishAffinity();
} catch (Exception e) {
// Ignore
}
Process.killProcess(Process.myPid()); |
It works fine, I keep it instead of my changes, thank you. |
My use case is : I am looking for existing script called "Eduphone_initTX.sh", I first search in SD card or USB if exists, then in /sdcard in not. If I find this sh I execute it. This task is in a small sh put in profile.d of bootstrap, it does nothing if application directory already exists. The SD card or otg USB contains the whole data for Eduphone installation ( mainly tar.gz of the the working environment, and some apk). Eduphone_initTX.sh make the installation : install the apk (termux-api, termux-widget, a wifi hot-spot for old smartphone, my apk for managing the configuration) and restore the tar.gz |
You are welcome. By sd card do you mean, "sd card that gets inserted into sd card slot of phone" or "sd card connected with usb otg cable and external sd card reader"? |
By sd card I mean "sd card that gets inserted into sd card slot of phone".
nb : sorry for my poor bash :) |
Can you send output for Also can you access the sd card with |
I test on a Samsung A326B On SD card :
On otg
Notice that this point works fine. |
Thanks and what's the output of |
|
Are you on Android 12 on this device as stated before? |
Android 12 |
…`>= 11` when running `termux-setup-storage` Requesting `MANAGE_EXTERNAL_STORAGE` should additionally grant access to unreliable/removable volumes like USB OTG devices under the `/mnt/media_rw/XXXX-XXXX` paths on `Android >= 12`, so request that if possible. Check #71 (comment) for more info. Fixes issue on Android `14`, where using `targetSdkVersion=28`, that requests the legacy `WRITE_EXTERNAL_STORAGE` will actually request the `photos, music, video, and other files` permissions (`READ_MEDIA_AUDIO`/`READ_MEDIA_IMAGES`/`READ_MEDIA_VIDEO`) and apparently access to full external storage `/sdcard` is not available for some users, maybe because `READ_EXTERNAL_STORAGE` and `WRITE_EXTERNAL_STORAGE` permissions are not granted for those device automatically in addition to `READ_MEDIA_*` permission. The issue is not reproducible on Android `13-15` avd. To solve this, we request the singular `MANAGE_EXTERNAL_STORAGE` permission instead so that full access is always available. Related: #3647 (comment) See also: - https://developer.android.com/training/data-storage/shared/media#access-other-apps-files - https://developer.android.com/reference/android/Manifest.permission#READ_MEDIA_IMAGES
Hi,
I am using Android 6 (cm13.0) and I have formatted my sd card as internal storage. I have already found https://termux.com/storage.html and when running termux-setup-storage I can acess my internal storage.
I also want to access a usb thumbdrive, which is visible in es explorer and the android storage settings, but will not be displayed beneath ~/storage (the folder external is missing as well).
Is this expected behaviour or is there something wrong on my device?
PS: the drive does show up in /storage btw. But trying to create a file on it will give a "permission denied"
The text was updated successfully, but these errors were encountered: