Note that there are some explanatory texts on larger screens.

plurals
  1. POProgrammatic screenshot on android: setDrawingCacheEnabled throws a CalledFromWrongThreadException
    primarykey
    data
    text
    <p>I'm running into an issue when trying to call content.setDrawingCacheEnabled(true) in one of my robotium tests. When i try to enable the drawing cache on the context I <strong>sometimes</strong> get a CalledFromWrongThreadException. This only seems to happen when running on an emulator. When I try this on device (Samsung S2) it works every time...</p> <p>Here is the code</p> <p><em><strong>Calling function:</em></strong></p> <pre><code>String screnshotName = String.format("SS_Cities_%s.png", s); File ssDir = Environment.getExternalStorageDirectory(); TestUtilities.takeScreenShot(solo.getCurrentActivity(), ssDir, screnshotName); </code></pre> <p><em><strong>Screenshot function (that throws exception):</em></strong></p> <pre><code>public static void takeScreenShot(Activity activity, File Directory, String FileName) throws Exception { View content = activity.findViewById(R.id.content); ***// This is a horrible hack that i want to get rid of*** // Occasiaonally setDrawingCacheEnabled throws a CalledFromWrongThreadException int MAX_RETRIES = 10; for(int i = 0; i &lt; MAX_RETRIES; i++) { try{ content.setDrawingCacheEnabled(true); continue; } catch (Exception e){} } content.buildDrawingCache(); Bitmap b = content.getDrawingCache(); File outputFile = new File(Directory.toString() + "/" + FileName); try { if (!Directory.exists()) { Directory.mkdirs(); } if (!Directory.canWrite()) { throw new Exception("Directory not writable"); } FileOutputStream fos = new FileOutputStream(outputFile); if (fos != null) { b.compress(Bitmap.CompressFormat.PNG, 90, fos); fos.close(); } } catch (Exception e) { String screenshotError = String.format("Error taking screenshot: %s", e.toString()); throw new Exception(screenshotError); } } </code></pre> <p><em><strong>Permissions set in the application I'm testing:</em></strong></p> <pre><code>&lt;uses-permission android:name="android.permission.CHANGE_CONFIGURATION"/&gt; &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload