我想就如何通过按下按钮捕获和保存使用 Phonegap 编译的 Android 应用程序中当前显示的内容征求意见。
我花了很多时间研究可能的方法来做到这一点:
创建一个 Phonegap 插件来查找当前 View ,将其转换为位图,然后将其保存到设备 SD 卡中。如所见here .
使用 HTML5 Canvas 标签并将此元素和任何子元素转换为 PNG 文件,使用 toDataURL 方法,如 here 所示.
我无所事事,非常感谢任何可能有帮助的建议、教程或链接。有数百篇 SO 帖子说这不能在 Android 中完成,但我不敢相信考虑到我已经找到了两个可能可行的链接。
我已经尝试了上面的链接,但是有一些突出的问题阻止我让它们工作。我正在寻找替代方案。
编辑:下面是我正在使用的插件代码,以及第一个链接中有关如何记录 View 屏幕截图的代码。
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);
}
}
我在这一行得到一个空指针异常:
View content = findViewById(R.id.rootlayout);
我的问题是,Phonegap 会在 Android 中创建 View 吗?如果是,您如何使用 findViewById 方法选择它们?
最佳答案
您对#1 有什么问题?从理论上讲,这应该有效。另一个更复杂的选项是创建自定义 ViewGroup,将 phonegap 的 Web View 放入其中,并覆盖绘图方法以生成可以保存的图像。这在概念上与 #1 相同,但需要做更多的工作。
关于javascript - 关于如何使用 Phonegap 在 Android 应用程序中捕获屏幕的建议?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8125978/