Wednesday, 4 September 2013

How to take snapshot for webkit on android nativelly?

How to take snapshot for webkit on android nativelly?

How to take snapshot for html page webkit on android nativelly?
It's simple...view my solution comprovated on one project for
orthodontics: 1) You create one WebView widget on main Activity class file
on onCreate method with url for HTML5 page; 2) Create one method for
printing content(printHTMLSnapshot); 3) Bind the method for printing with
one event click for example one Button widget including on your Button on
main_layout.xml that: android:onClick="printHTMLSnapshot" after element
android:id on Button tag. 4) Complete all code on method and path for save
image and task concluded for tests;
WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_archade_tooth);
String url ="file:///android_asset/xxxxxx.html";
wv=(WebView) findViewById(R.id.webView1);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setPluginState(PluginState.ON);
wv.loadUrl(url);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.archade_tooth, menu);
return true;
}
public void printHTMLSnapshot(View view) {
Log.e("MSG",">>> Begin printing...>>>");
Picture picture = wv.capturePicture();
Bitmap b = Bitmap.createBitmap(picture.getWidth(),
picture.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas( b );
picture.draw( c );
ByteArrayOutputStream stream = null;
byte[] byteArchadeImage = null;
Intent i = getIntent();
// ************ Patient is a bean that save stream for image file on
some local
Patient patient = (Patient) i.getSerializableExtra("patient");
try {
stream = new ByteArrayOutputStream();
if ( stream != null )
{
b.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byteArchadeImage = stream.toByteArray();
if (null!=patient) {
patient.setArchadeImage(byteArchadeImage);
}
stream.flush();
stream.close();
}
}
catch( Exception e )
{
Log.e("MSG", e.getLocalizedMessage());
} finally {
i.setClass(getBaseContext(), ServicesArchadeToothActivity.class);
i.putExtra("patient", patient);
startActivity(i);
}
Log.e("MSG",">>> Ended printing...>>>");
}

No comments:

Post a Comment