Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to take screenshot on android using robotium and private method

i have been trying to get a screenshot lately but every thing in vain the folders are created in android emulator with api level 8. i have mentioned the code below.

In the this code Method takeScreenShot() is supposed to create a directory and store the image while executing as android junit testcase i get result as 100% but not the folders are not Created and screen shot is not stored. should i root my phone to use its sd card ?

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) {
                }
            }
        });

    }

}
like image 820
Daniel Euchar Avatar asked Sep 26 '11 02:09

Daniel Euchar


People also ask

How do you take a screenshot on an Android Virtual Device?

On many Android devices, you can capture a screenshot with a key-combination: Simultaneously press-and-hold Power and Volume-down. You can also capture a screenshot with Android Studio as follows: Run your app on a connected device or emulator. If using a connected device, be sure you have enabled USB debugging.


1 Answers

You need add permission to write to the SD Card in the main application. Not the JUnit test project! Add this to the project manifest: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

like image 61
Starter Avatar answered Oct 19 '22 23:10

Starter