无法使用robotium和私有方法在android上截屏

Dan*_*har 5 java io android robotium

我最近一直试图获取屏幕截图,但是每个东西都是徒劳的,文件夹是在api level 8的android模拟器中创建的.我已经提到了下面的代码.

在这段代码中,方法takeScreenShot()应该创建一个目录并存储图像,同时作为android junit testcase执行我得到的结果为100%但不是文件夹没有创建并且没有存储屏幕截图.我应该根据我的手机使用其SD卡吗?

public class NewRobotiumTest extends ActivityInstrumentationTestCase2 {
......
......

    // actual testcase

    public void testRecorded() throws Exception {
        solo.waitForActivity("com.botskool.DialogBox.DialogBox",
                ACTIVITY_WAIT_MILLIS);
        solo.clickOnButton("Show Alert");
        solo.clickOnButton("Ok");
        solo.clickOnButton("Show Yes/No");
        takeScreenShot(solo.getViews().get(0), "testRecorded_1316975601089");
        solo.sleep(2000);
        solo.clickOnButton("Yes");
        solo.clickOnButton("Show List");
        solo.clickOnScreen(118f, 563f);

    }

    /**
     * I have added this to the android-manifest.xml file
     *
     * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     *  
     */

    public void takeScreenShot(final View view, final String name)
            throws Exception {

        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                view.setDrawingCacheEnabled(true);
                view.buildDrawingCache();
                Bitmap b = view.getDrawingCache();
                FileOutputStream fos = null;
                try {
                    final String path = Environment.getExternalStorageDirectory()+ "/test-screenshots/";
                    File dir = new File("/mnt/sdcard/test-screenshots");
                    if(!dir.mkdirs()){
                        System.out.println("Creaet sd card failed");
                    }

                    if (!dir.exists()) {
                        System.out.println(path);
                        dir.mkdirs();
                    }

                    fos = new FileOutputStream(path + name + ".jpg");
                    if (fos != null) {
                        b.compress(Bitmap.CompressFormat.JPEG, 90, fos);
                        fos.close();
                    }
                } catch (IOException e) {
                }
            }
        });

    }

}
Run Code Online (Sandbox Code Playgroud)

小智 7

您需要添加权限才能在主应用程序中写入SD卡.不是JUnit测试项目!将其添加到项目清单中: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>