简体   繁体   English

Android OS 11+ 将数据文件推送到应用程序范围的存储

[英]Android OS 11+ pushing data files to an applications scoped storage

I have an Android algorithm library that I want to test inside an application that can run on real device.我有一个 Android 算法库,我想在可以在真实设备上运行的应用程序中进行测试。 In order to run this test, I need to be able to push data files from a computer to the phone, to a location that can be read by the application being tested.为了运行这个测试,我需要能够将数据文件从计算机推送到手机,推送到正在测试的应用程序可以读取的位置。 Before OS 11 this could be done using the adb push... command but the new scoped storage privacy feature will not allow me to do this anymore.在 OS 11 之前,这可以使用adb push...命令完成,但新的范围存储隐私功能将不再允许我这样做。

Whatever the solution is, it can not require manual human intervention as the intention of this test is to run automatically as part of a CI/CD pipeline.无论解决方案是什么,它都不需要人工干预,因为该测试的目的是作为 CI/CD 管道的一部分自动运行。

Does anyone have any solutions?有没有人有任何解决方案? Thanks in advance for your suggestions!提前感谢您的建议!

You can add this permission to the Manifest:您可以将此权限添加到清单中:

<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

To grant this permission you need to enable it, but as far as I know this cannot be changed programatically, but you can use this adb command to change it via UI (Or you can write an UI Automator script to be more robust):要授予此权限,您需要启用它,但据我所知,这不能以编程方式更改,但您可以使用此 adb 命令通过 UI 更改它(或者您可以编写 UI Automator 脚本以使其更健壮):

adb shell "am start -a android.settings.MANAGE_APP_ALL_FILES_ACCESS_PERMISSION \
       -d package:<REPLACE_WITH_YOUR_PACKAGE_NAME> ; \
       sleep 2 ; \
       input keyevent KEYCODE_DPAD_DOWN ; \
       input keyevent KEYCODE_DPAD_DOWN ; \
       input keyevent KEYCODE_DPAD_CENTER"

Now you can read your files normally:现在您可以正常读取文件:

@Test
fun myTest() {
    val content = File("/sdcard/Download/myFile.txt")
        .useLines(Charsets.UTF_8) {
            it.joinToString("\n")
        }
    Log.d("TAG", content)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM