android file provider: Example to access files in a folder of app private directory(/data/data/<pkg_name>)

孤街醉人 提交于 2019-12-13 01:30:01

问题


I am running android test using Cucumber.The reports of the test are being stored in /data/data/pkg_name/cucumber-reports.Now I need that content inside "cucumber-reports" folder.I used copy command to copy the cucumber reports folder from internal storage to Public External Directory(PICTURES) which is working in Marshmallow.But when I ran in Nougat,I am unable to copy that folder to External Directory.I am unable to access the folder on Android 7+ version device as due to the introduction of new file permission changes.

Without FileProviders,I am using execute Shell command of Instrumentation to copy"cp" folder from internal app dir to External Dir. Please find the copy code below:

   public static void copyReportsToPublicDirs(Scenario scenario) {
        try {
            File externalStoragePublicDirectory=  Environment.getExternalStoragePublicDirectory(DIRECTORY_PICTURES);
            String extStrPath= externalStoragePublicDirectory.getPath()+"/"+"cucumber-reports";
            new File(extStrPath).mkdirs();
            Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
            String command = "cp -R -f "+"/data/data/"+ BuildConfig.APPLICATION_ID+"/cucumber-reports "+extStrPath;
            Log.println(Log.INFO,"Copy Reports to SD Card","Performing copy the reports to SD Card");
            ParcelFileDescriptor pfd =instrumentation.getUiAutomation().executeShellCommand(command);
            if(pfd.canDetectErrors())
            Log.println(Log.INFO,"Error displayed","Copy Error");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

The above code works fine on Marshmallow but on Nougat,I am not seeing the folder and its contents not copied from it. By what ways,can I retrieve the cucumber report folder inside data/data to External Dir?


回答1:


What you need is here: https://stackoverflow.com/a/15559278/8361368
You can write test bash script like this:

adb install your.target.apk
adb install your.cucumber.apk
adb shell am instrument ...
adb backup -f xxx.ab $YOUR_CUCUMBER_PACKAGE_NAME
dd if=xxx.ab bs=1 skip=24 | python -c "import zlib,sys;sys.stdout.write(zlib.decompress(sys.stdin.read()))" | tar -xvf -

Then you got your whole cucumber report files on your host dirs.



来源:https://stackoverflow.com/questions/47807052/android-file-provider-example-to-access-files-in-a-folder-of-app-private-direct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!