有关如何使用Phonegap在Android应用中捕获屏幕的建议?

时间:2011-11-14 18:03:03

标签: javascript android mobile cordova

我正在询问有关如何通过按下按钮来捕获和保存使用Phonegap编译的Android应用程序中当前显示的内容的建议。

我已经花了几个小时研究可能的方法来做到这一点:

  1. 创建一个找到当前视图的Phonegap插件,将其转换为位图,然后将其保存到设备SDcard。见here

  2. 使用HTML5 Canvas标记,并使用to here中的toDataURL方法将此元素和任何子元素转换为PNG文件。

  3. 我处于松散状态,非常感谢任何可能有用的建议,教程或链接。有数百个SO帖子说这在Android中无法完成,但我无法相信,考虑到我找到了两个可能的链接。

    我已经尝试过上面的链接,但是有一些突出的问题阻止我让他们工作。我一直在寻找其他选择。

    编辑:下面是我正在使用的插件代码,以及第一个链接中有关如何记录视图截图的代码。

    package com.company.msgbox;
    
    //imports
    
    @Override
    public PluginResult execute(String arg0, final JSONArray arg1, String arg2) {
    if ( arg0.equals(SHOW) )
    {
    this.ctx.runOnUiThread(new Runnable()
    {
    public void run()
    {
    // try/catch generated by editor
    try {
    msg = arg1.getString(MSG_INDEX);
    } catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    
    AlertDialog alertDialog = new AlertDialog.Builder(ctx).create();
    alertDialog.setTitle("Title");
    alertDialog.setMessage(msg);
    alertDialog.show();
    
    //screen shot
    View content = findViewById(R.id.rootlayout);
    Bitmap bitmap = content.getDrawingCache();
    File file = new File("/sdcard/test.png");
    try 
    {
        file.createNewFile();
        FileOutputStream ostream = new FileOutputStream(file);
        bitmap.compress(CompressFormat.PNG, 100, ostream);
        ostream.close();
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }
    
    
    }//end of scope
    });
    }
    
    return new PluginResult(Status.OK);
    }
    
    }
    

    我在这一行得到了一个N​​ull Pointer Exception:

    View content = findViewById(R.id.rootlayout);
    

    我的问题是,Phonegap会在Android中创建视图,如果是这样,你如何使用findViewById方法选择它们?

2 个答案:

答案 0 :(得分:1)

你对#1有什么问题?从理论上讲,这应该有效。另一个更复杂的选项是创建自定义ViewGroup,将phonegap的Web视图放在其中,并覆盖绘图方法以生成可以保存的图像。这在概念上与#1相同,但还有更多的工作。

答案 1 :(得分:1)

这篇文章可能会有所帮助:http://uihacker.blogspot.com/2011/01/android-phonegap-scale-webview-to-fit.html 这篇文章暗示整个应用程序的webview可以通过Activity对象的appView属性。例如,在我的代码中:

public class SampleActivity extends DroidGap implements OnTouchListener{
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.loadUrl("file:///android_asset/www/index.html");
        this.appView.setOnTouchListener((OnTouchListener)this);
   //   this.appView is the biggest view that wrap the whole android application 
   }

  ...implement other methods...
}