android file provider: Example to access files in

2019-07-30 03:35发布

问题:

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.