Which can replace capturePicture function
I finally found out the solution.
Some of codes
public class WebViewActivity extends Activity {
private static WebView webView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
webView = (WebView) findViewById(R.id.webView1);
webView.loadUrl("http://developer.android.com/reference/packages.html");
// webView.loadUrl("http://developer.android.com/training/basics/firstapp/creating-project.html");
webView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
webView.measure(MeasureSpec.makeMeasureSpec(
MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
webView.layout(0, 0, webView.getMeasuredWidth(),
webView.getMeasuredHeight());
webView.setDrawingCacheEnabled(true);
webView.buildDrawingCache();
Bitmap bm = Bitmap.createBitmap(webView.getMeasuredWidth(),
webView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas bigcanvas = new Canvas(bm);
Paint paint = new Paint();
int iHeight = bm.getHeight();
bigcanvas.drawBitmap(bm, 0, iHeight, paint);
webView.draw(bigcanvas);
System.out.println("1111111111111111111111="
+ bigcanvas.getWidth());
System.out.println("22222222222222222222222="
+ bigcanvas.getHeight());
if (bm != null) {
try {
String path = Environment.getExternalStorageDirectory()
.toString();
OutputStream fOut = null;
File file = new File(path, "/aaaa.png");
fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 50, fOut);
fOut.flush();
fOut.close();
bm.recycle();
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
}
The layout.xml
<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
How to capture a webview to bitmap in Android?
I found out the solution. Take a look of this.
I used the onDraw to replace the capturePicture functon which is deprecated in API 19. If you have any better solutions, please tell me and I appreciate that. Thanks!
Which can replace capturePicture function
Snapshot the webview return blank image
I found out the solution. Take a look of this.
I used the onDraw to replace the capturePicture functon which is deprecated in API 19. If you have any better solutions, please tell me and I appreciate that. Thanks!
Which can replace capturePicture function
Android best way to generate an image from a layout engine (justified text, box positioning, embed images...)
I finally succeeded in using a WebView without having to see the view. I set the width to 0.1dp and switch visible/invisible quickly.
I use a WebView because the layout engine is more complete. But warning : as mentioned higher, the method onNewPicture is deprecated.
I implemented a class containing a GenerationProcess which perform all Thread treatments (on the UI Thread or on a backgroud Thread) and a GenerationQueue which perform a Thread ordering. So this class is totally independent and you can use it without having to think about the Thread you are.
With the Android Layout engine, we don't have to use all these Thread because there is no callback function like onPageFinished and onNewPicture : make your choice !
Github Project - WebView2Img
You can take a look at :
- ./src containing the Activity example and the class Page
- ./res/layout containing the layout example
- ./assets containing all html example
Then you can extends Page to manipulate the DOM (with jdom for example) to dynamicaly set content and save the html file.
EDIT : here an example of extends Page with dom manipulation before generation
How to print whole WebView(Scrollable)?
Draw the DecorView object of the current window, and then draw the Bitmap object.
And you can do like this.
Before doing it ,you can do this.
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webView.requestFocusFromTouch();
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webView.loadUrl("your url");
And check version.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkSdkVersion();
setContentView(R.layout.activity_webview_capture);
}
// check version
private void checkSdkVersion() {
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
WebView.enableSlowWholeDocumentDraw();
}
}
Add permission in your manifest.
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Way 1
private void getScreenshot() {
float scale = webView.getScale();
int webViewHeight = (int) (webView.getContentHeight() * scale + 0.5);
Bitmap bitmap = Bitmap.createBitmap(webView.getWidth(), webViewHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
webView.draw(canvas);
// save to the File
try {
String fileName = Environment.getExternalStorageDirectory().getPath() + "/webview_capture1.jpg";
FileOutputStream fos = new FileOutputStream(fileName);
// Save
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
fos.close();
Toast.makeText(WebviewFromDraw.this, "Screenshot OK", Toast.LENGTH_LONG).show();
bitmap.recycle();
} catch (Exception e) {
e.getMessage();
}
}
Way 2 use webView.getDrawingCache();
private void getScreenshot() {
bitmap = webView.getDrawingCache();
try {
String fileName = Environment.getExternalStorageDirectory().getPath()+"/webview_capture2.jpg";
FileOutputStream fos = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
fos.close();
Toast.makeText(WebviewFromDrawCache.this, "Screenshot OK", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e("TAG", e.getMessage());
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//recycle
if(bitmap!=null) {
bitmap.recycle();
}
}
Way 3 the same to yours,it works well in my device.
private void getScreenshot() {
Picture picture = webView.capturePicture();
int width = picture.getWidth();
int height = picture.getHeight();
if (width > 0 && height > 0) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
picture.draw(canvas);
try {
String fileName = Environment.getExternalStorageDirectory().getPath()+"/webview_capture3.jpg";
FileOutputStream fos = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
fos.close();
Toast.makeText(WebviewFromCapture.this, "Screenshot ok", Toast.LENGTH_LONG).show();
bitmap.recycle();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
}
Related Topics
Android Background Drawable Not Working in Button Since Android Studio 4.1
How to Check If "Android.Permission.Package_Usage_Stats" Permission Is Given
How to Hide Toolbar When I Scrolling Content Up
Adding Your Own SQLite Database to an Android Application
How to Kill Sub Activities and Bring Activity to Top of Stack
Navigation Drawer with Backword Compatibility Android
Circular Reveal Transition for New Activity
Adding Fling Gesture to an Image View - Android
Httpurlconnection Worked Fine in Android 2.X But Not in 4.1: No Authentication Challenges Found
Send Post Request with Params Using Retrofit
Error:Execution Failed for Task ':App:Transformresourceswithmergejavaresfordebug'
How to Add Android Support V7 Libraries in Eclipse
Error:Connectionresult{Statuscode=Internal_Error, Resolution=Null}
How to Disable Facebook Single Sign on for Android - Facebook-Android-Sdk