简体   繁体   中英

Can I use robotium's take 'snapshot' method in an android Proj. If so any suggestions to how?

I need to take some screen shots, is it possible to use the method 'takeScreenShot' already available in Robotium to take screen shots? I've imported the jar files, but I haven't been too successful. If it is not possible to use robotium can you suggest any other solutions.

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: unable to resolve new-instance 468 (Lcom/jayway/android/robotium/solo/Solo;) in Lcom/example/unboundserviceex/MyService; 03-06 17:27:54.949: D/dalvikvm(1405): VFY: replacing opcode 0x22 at 0x0000 03-06 17:27:54.949: D/dalvikvm(1405): DexOpt: unable to opt direct call 0x0cf0 at 0x04 in Lcom/example/unboundserviceex/MyService;.onStartCommand 03-06 17:27:54.949: I/My Service(1405): Instrumentation Obj was created 03-06 17:27:54.959: D/AndroidRuntime(1405): Shutting down VM 03-06 17:27:54.959: W/dalvikvm(1405): threadid=1: thread exiting with uncaught exception (group=0x409c01f8) 03-06 17:27:54.979: E/AndroidRuntime(1405): FATAL EXCEPTION: main

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): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2359) 03-06 17:27:54.979: E/AndroidRuntime(1405): at android.app.ActivityThread.access$1900(ActivityThread.java:123) 03-06 17:27:54.979: E/AndroidRuntime(1405): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210) 03-06 17:27:54.979: E/AndroidRuntime(1405): at android.os.Handler.dispatchMessage(Handler.java:99) 03-06 17:27:54.979: E/AndroidRuntime(1405): at android.os.Looper.loop(Looper.java:137) 03-06 17:27:54.979: E/AndroidRuntime(1405): at 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:2 7: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)

The main error I believe is: java.lang.NoClassDefFoundError

Robotium is able to takeScreenshot, but it will contain only views of your application (for instance status bar will be blank). What problem did you encounter? "I haven't been too successful" says nothing.

EDIT: It seems that, you don't have robotium-solo jar included to your project.

Anyway, if you need it only to taking screenshots, you don't need it at all, just use this code, but as I wrote before, you will need any view to take it (and permission to write on external storage)

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

If one wants to use Robotium frame work in their project one should also understand that Robotium requires 2 things:

  1. The activity
  2. The package name

Next step, you need to add these details into the manifest file (in the instrumentation tag)

Now hang on the above detail.

We need to add these two details dynamically into manifest of the screenshot recorder application

which is not possible since defeats the purpose of a manifest file

Essentially I'm saying that this was a bad idea and we cannot use Robotium's 'takeScreenShot' method to take screen shots. I recommend using the ASL Library , which I am still having trouble working with. Thank you for your time and my apologies for wasting someone else'.

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