android 上的编程屏幕截图:setDrawingCacheEnabled 抛出 CalledFromWrongThreadException

发布于 2024-11-24 22:36:21 字数 2006 浏览 2 评论 0原文

在我的一个 robotsium 测试中尝试调用 content.setDrawingCacheEnabled(true) 时,我遇到了问题。当我尝试在上下文中启用绘图缓存时,我有时会收到 CalledFromWrongThreadException。这似乎只发生在模拟器上运行时。当我在设备(三星 S2)上尝试此操作时,它每次都有效...

这是代码

调用函数:

String screnshotName = String.format("SS_Cities_%s.png", s);
File ssDir = Environment.getExternalStorageDirectory();
TestUtilities.takeScreenShot(solo.getCurrentActivity(), ssDir, screnshotName);

屏幕截图函数(抛出异常):

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

我正在测试的应用程序中设置的权限:

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

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 sometimes 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...

Here is the code

Calling function:

String screnshotName = String.format("SS_Cities_%s.png", s);
File ssDir = Environment.getExternalStorageDirectory();
TestUtilities.takeScreenShot(solo.getCurrentActivity(), ssDir, screnshotName);

Screenshot function (that throws exception):

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

Permissions set in the application I'm testing:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

涫野音 2024-12-01 22:36:21

Android 模拟器在使用外部存储时并不总是能正常工作。这就是您可能会收到这些错误的原因。因此,我建议每当您想要测试使用外部存储的应用程序时,都在真实设备中进行,因为您不会遇到模拟器仍然存在的所有“未修复”错误。

Android emulator doesn't always work fine with external storage. That's why you might get those errors. So I suggest that whenever you want to test apps that use external storage you do it in a real device since you won't have all the "unfixed" errors that the emulator still has.

顾忌 2024-12-01 22:36:21

我看不到您是否从 UIThread 执行 takeScreenShot 方法,但据我所知,我认为您的问题在这里得到了解答:

StackOverflow:CalledFromWrongThreadException

I can't see whether you perform your takeScreenShot method from the UIThread or not but as far as I can see I think your question is being answered here:

StackOverflow: CalledFromWrongThreadException

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文