简体   繁体   中英

How to create screenshot in Robotium

I'd like to know how can I take screenshot on AVD in Robotium. I have read that 1st I have to have permissions but I've already done this.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />

But still I have error on Android Monitor

D/Robotium: Can't save the screenshot! Requires write permission (android.permission.WRITE_EXTERNAL_STORAGE) in AndroidManifest.xml of the application under test.
W/System.err: java.io.FileNotFoundException: /sdcard/Screenshot/asd.jpg: open failed: EACCES (Permission denied)

I created manually directory to be sure that dir exist.

Also I was trying to take screenshot on my disk

String path = "/sdcard/SS";
solo.getConfig().screenshotSavePath = path;
solo.takeScreenshot("asd");

path = "C:/";
solo.getConfig().screenshotSavePath = path;
solo.takeScreenshot("asd");

but error still exist. What's wrong with taking screenshot?

如果您已经创建了SS目录:

String path = Environment.getExternalStorageDirectory().getPath() + "/SS";

First of all, create your path, as @Seishin said:

String path = Environment.getExternalStorageDirectory().getPath() + "/SS";

Then, go to your build.gradle file and downgrade targetSdkVersion to 22 .

Higher than 22 , I mean: 23 or 24 , requires to define Android Permissions Control, which is a bit different than defining only:

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

Read: https://developer.android.com/training/permissions/index.html

These kind of permissions (runtime permissions) work on Android 6.0 and higher version and the needed code looks like:

// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
                Manifest.permission.READ_CONTACTS)
        != PackageManager.PERMISSION_GRANTED) {

    // Should we show an explanation?
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
            Manifest.permission.READ_CONTACTS)) {

        // Show an expanation to the user *asynchronously* -- don't block
        // this thread waiting for the user's response! After the user
        // sees the explanation, try again to request the permission.

    } else {

        // No explanation needed, we can request the permission.

        ActivityCompat.requestPermissions(thisActivity,
                new String[]{Manifest.permission.READ_CONTACTS},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);

        // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
        // app-defined int constant. The callback method gets the
        // result of the request.
    }
}

To avoid it, just downgrade your targetSdkVersion .

Hope it will help

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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