I'm asking for suggestions on how you would go about capturing and saving what is currently displayed in an Android application compiled with Phonegap, by pressing a button.
I've done hours and hours of research into possible ways to do this:
Creating a Phonegap plugin that finds the current view, converts it to a bitmap then saves this to the devices SDcard. As seen here.
Uses the HTML5 Canvas tag and converts this and any child elements to a PNG file, using the toDataURL method as seen here.
I'm at a loose end and would really appreciate any suggestions, tutorials or links to anything that may help. There are hundreds of SO posts saying this cannot be done in Android, but I can't believe that considering i've found two links that may be possible.
I have tried the links above, however there are some outstanding issues that prevent me from getting them to work. I was looking for alternatives.
Edit: Below is the plugin code I am using, together with the code from the first link on how to record a screenshot of a 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);
}
}
I get a Null Pointer Exception at this line:
View content = findViewById(R.id.rootlayout);
My question would be, does Phonegap create views in Android and if so, how do you select them using the findViewById method?