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

标签 android adb

我正在尝试将文件推送到 Android 设备,然后在测试应用程序中读取它。文件在哪里并不特别重要,我可以在应用程序中硬编码路径,但文件必须单独写入设备。如果我使用 adb 将文件推送到某处,则该文件由 root 拥有,权限为 660,并且应用程序无法打开它。如果我run-as应用程序并尝试读取文件,我会收到“权限被拒绝”。如果我尝试 chmod 该文件,我会收到“不允许操作”。除了 root 设备之外,还有其他方法可以做到这一点吗?

最佳答案

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

根据您的情况,您可以按照以下方式进行操作。

# 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 手机上测试。

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

引用此评论answer.

关于android - 将应用程序可读的文件推送到 Android 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31390118/

相关文章:

android - 自 SDK API 9 升级以来,DDMS 抛出 ADB 错误

java - 为什么在调用 onDestroy() 时服务不停止

android - 绑定(bind)服务崩溃并出现 "Context.startForegroundService() did not then call Service.startForeground()"错误

java - 使用 flatMap 和过滤器过滤可观察对象是否正确?

java - 如何创建第二个类并将Test设置为TextView?

安卓扩展文件

安卓网络地址

android - 没有一致的方式通过 TCP 连接 ADB

android - 无法将 Nexus 4 连接到 adb : unauthorized

java - react native Android 语法错误 : Attempted to redefine property 'key'