【问题标题】:Suggestions on how to capture screen in Android app using Phonegap?关于如何使用 Phonegap 在 Android 应用程序中捕获屏幕的建议?
【发布时间】:2011-11-14 18:03:02
【问题描述】:

我想请教您如何通过按一个按钮来捕获和保存使用 Phonegap 编译的 Android 应用程序中当前显示的内容。

我对可能的方法进行了数小时的研究:

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

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

我很忙,非常感谢任何可能有帮助的建议、教程或链接。有数百个 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);
}

}

我在这一行得到一个空指针异常:

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

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

【问题讨论】:

  • “有数百个 SO 帖子说这不能在 Android 中完成,但考虑到我发现了两个可能的链接,我不敢相信。” ——这些帖子中的大多数都是针对试图捕获任何应用程序活动的屏幕截图的人,而不仅仅是他们自己的。这是不支持的。您的方法仅适用于您的活动,并且它们受到支持,只是不是特别好。 :-)
  • @LordSnoutimus: findViewById()Activity 上的一个方法。我不知道 PhoneGap 插件架构。 ctx 可能是您的 Activity。如果没有,请联系 PhoneGap 人员,了解如何通过插件访问您的 Activity。我希望这是可能的。
  • @LordSnoutimus:如果它还不是Activity,您必须将其转换为Activity。与 JavaScript 不同,Java 是强类型的。但是你会打电话给ctx.findViewById(R.id.rootlayout)(如果ctxActivity)或((Activity)ctx).findViewById(R.id.rootlayout)(如果ctx真的是Activity,但作为更通用的Context交给你)。如果ClassCastException 失败,那么ctxContext 的其他类型,而不是Activity,你又回到了原点。
  • @LordSnoutimus:太棒了——我从来没有访问过绘图缓存。我建议你就这个话题提出一个新问题。
  • @LordSnoutimus:尝试R.id.appView,基于github.com/callback/callback-android/blob/master/framework/res/…

标签: javascript android mobile cordova


【解决方案1】:

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

【讨论】:

  • 凯文您好,感谢您的回复。好吧,我创建了一个Phonegap 插件来显示一个警报作为一个有效的测试,当尝试使用FindByViewId 查找主要Phonegap 活动的根视图时出现问题。 phonegap 是否会原生创建 Web 视图?
  • @LordSnoutimus:不确定您所说的“本地”是什么意思。 PhoneGap 有一个带有WebView 的活动。您可以在您的 PhoneGap 项目 IIRC 中查看它的 Java 源代码。
  • @CommonsWare:我将更新我的原始帖子以包含一些代码和我遇到的错误。谢谢。
  • @Kevin Galligan:请查看我原帖中的更新代码。
【解决方案2】:

这篇文章可能对: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...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 2018-11-03
    • 2012-01-23
    • 2019-01-14
    • 2020-09-10
    • 2021-01-30
    • 2011-09-13
    相关资源
    最近更新 更多