我可以在android Proj中使用robotium的'快照'方法。若有,建议如何?

时间:2013-03-06 06:54:54

标签: android robotium

我需要拍摄一些屏幕截图,是否可以使用Robotium中已有的“takeScreenShot”方法拍摄屏幕截图?我已经导入了jar文件,但我没有太成功。 如果无法使用robotium,您可以建议任何其他解决方案。

public class MyService extends Service {
    Solo solo;
    Context con;
    private Instrumentation it;
    @Override
public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();

        it = new Instrumentation();
        Log.i("My Service", "Instrumentation Obj was created");

    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub

        solo = new Solo(it);
        if(solo == null)
        Log.i("My Service", "Solo Obj was created");
        solo.takeScreenshot();
        return super.onStartCommand(intent, flags, startId);
    }

Log cat O / P: 03-06 17:27:54.939:W / dalvikvm(1405):VFY:无法在Lcom / example / unboundserviceex / MyService中解析新实例468(Lcom / jayway / android / robotium / solo / Solo;); 03-06 17:27:54.949:D / dalvikvm(1405):VFY:在0x0000处替换操作码0x22 03-06 17:27:54.949:D / dalvikvm(1405):DexOpt:无法选择在Lcom / example / unboundserviceex / MyService中的0x04处直接调用0x0cf0; .onStartCommand 03-06 17:27:54.949:I / My Service(1405):Instrumentation Obj已创建 03-06 17:27:54.959:D / AndroidRuntime(1405):关闭VM 03-06 17:27:54.959:W / dalvikvm(1405):threadid = 1:线程退出时未捕获异常(group = 0x409c01f8) 03-06 17:27:54.979:E / AndroidRuntime(1405):致命异常:主

  

03-06 17:27:54.979:E / AndroidRuntime(1405):   java.lang.NoClassDefFoundError:

com.jayway.android.robotium.solo.Solo 03-06 17:27:54.979:E / AndroidRuntime(1405):at com.example.unboundserviceex.MyService.onStartCommand(MyService.java:33) 03-06 17:27:54.979:E / AndroidRuntime(1405):在android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2359) 03-06 17:27:54.979:E / AndroidRuntime(1405):在android.app.ActivityThread.access $ 1900(ActivityThread.java:123) 03-06 17:27:54.979:E / AndroidRuntime(1405):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1210) 03-06 17:27:54.979:E / AndroidRuntime(1405):在android.os.Handler.dispatchMessage(Handler.java:99) 03-06 17:27:54.979:E / AndroidRuntime(1405):在android.os.Looper.loop(Looper.java:137) 03-06 17:27:54.979:E / AndroidRuntime(1405):在android.app.ActivityThread.main(ActivityThread.java:4424) 03-06 17:27:54.979:E / AndroidRuntime(1405):at java.lang.reflect.Method.invokeNative(Native Method) 03-06 17:27:54.979:E / AndroidRuntime(1405):at java.lang.reflect.Method.invoke(Method.java:511) 03-06 17:27:54.979:E / AndroidRuntime(1405):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) 03-06 17:27:54.979:E / AndroidRuntime(1405):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 03-06 17:27:54.979:E / AndroidRuntime(1405):at dalvik.system.NativeStart.main(Native Method)

我认为的主要错误是:java.lang.NoClassDefFoundError

2 个答案:

答案 0 :(得分:0)

Robotium能够获取截图,但它只包含应用程序的视图(例如状态栏将为空白)。你遇到了什么问题? “我没有太成功”没有说什么。

编辑: 看来,你的项目中没有包含robotium-solo jar。

无论如何,如果你只需要截图,你根本不需要它,只需使用这个代码,但正如我之前写的那样,你需要任何视图来获取它(以及在外部存储上写入的权限) )

protected void takeScreenshot(String name, View v) {
    View view = v.getRootView();
    view.setDrawingCacheEnabled(true);
    view.buildDrawingCache();
    Bitmap bmp = view.getDrawingCache();
    if (bmp != null) {
        String path = String.format("%s/%s/",
            Environment.getExternalStorageDirectory(),
            "scrrenshots");

        File dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(String.format(
                    "%s%s.png", path, name));
            bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);
        } catch (IOException e) {
        } finally {
            if (view != null) {
                view.destroyDrawingCache();
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                }
            }
        }
    } 
}

答案 1 :(得分:0)

如果想在他们的项目中使用Robotium框架工作,人们也应该理解Robotium需要两件事:

  1. 活动
  2. 包名称
  3. 下一步,您需要将这些详细信息添加到清单文件中(在检测标记中)

    现在请看上面的细节。

    我们需要将这两个细节动态添加到屏幕截图记录器应用程序

    的清单中,这是不可能的,因为它会破坏清单文件的目的

    基本上我说这是一个坏主意,我们不能使用Robotium的'takeScreenShot'方法来拍摄屏幕截图。我建议使用ASL Library,我仍然无法使用。感谢您的时间和我为浪费别人而道歉。