将应用程序可读文件推送到 Android 设备

Push application-readable file to Android device

我正在尝试将文件推送到 Android 设备,然后在测试应用程序中读取它。文件在哪里并不特别重要,我可以在应用程序中对路径进行硬编码,但文件必须单独写入设备。如果我使用 adb 将文件推送到某处,它由 root 拥有,权限为 660,应用程序无法打开它。如果我 run-as 应用程序并尝试读取文件,我会得到 "Permission denied"。如果我尝试对文件进行 chmod,我会得到 "Operation not permitted"。除了对设备进行 root 之外,还有其他方法可以做到这一点吗?

您可以利用应用程序的内部私有存储(通常存在于 /data/local/ 下,具有明确的 adb shell 用户访问权限)。

对于你的情况,你可以按照下面的方式进行。

# Create your file (On Host PC) #
$ touch hello.txt

# Push it to the device (If the below path doesnt exist, it will create it) #
$ adb push hello.txt /data/local/tmp
  0 KB/s (14 bytes in 0.043s)

# Switch to ADB Shell #
$ adb shell

# See Permissions before applying chmod #
shell@android:/ $ ls -l /data/local/tmp/
-rw-rw-rw- shell    shell          14 2015-07-14 15:35 hello.txt

# Change permissions using chmod # 
shell@android:/ $ chmod 777 /data/local/tmp/hello.txt

# See Permissions after applying chmod #
shell@android:/ $ ls -l data/local/tmp/
-rwxrwxrwx shell    shell          14 2015-07-14 15:35 hello.txt

在非 root Android phone 上测试。

Android  OS : 5.0.2  
ADB version : Android Debug Bridge version 1.0.31

参考评论answer.