Android的:取一个网页的“屏幕截图”从后台服务? [英] Android: take a 'screenshot' of a web page from a background service?
问题描述
我有一个网页的URL,我想借此这个网页的屏幕截图的背景下,如。在服务,和没有显示的UI给用户。
I have a URL for a web page, and I want to take a 'screenshot' of this web page in the background, eg. in a Service, and without showing a UI to the user.
我试图在我的服务创建一个web视图,然后使用 capturePicture()
方法来获得在页面加载完成后的截图,但创建图片
(和位图
我用它创建)总是空的。 (这工作完全在正常的活动,但不是在我的后台服务)。
I have tried to create a WebView in my Service, and then use the capturePicture()
method to get the screenshot when the page has finished loading, but the created Picture
(and the Bitmap
I create from it ) is always empty. (This works perfectly in a normal Activity, but not in my background Service).
任何方式得到这个工作,或者另一种方式来获得一个网页的截图,而无需一个UI?
Any way to get this to work, or an alternative way to get a 'screenshot' of a webpage without having a UI ?
推荐答案
想通了,我必须设置web视图的大小,这样所产生的屏幕截图不是0 X 0的大小。然后,我必须直接从获得的位图的WebView的绘制缓存的,如 capturePicture()
似乎并没有在这里工作。
Figured it out, I have to set the 'size' of the WebView, so that the resulting 'Screenshot' is not 0 x 0 size. Then I have to get the bitmap directly from the WebView's drawing cache, as capturePicture()
does not seem to work here.
package com.example.screenshot;
import android.app.*;
import android.content.*;
import android.widget.*;
import android.util.*;
import android.webkit.*;
import android.graphics.*;
import java.io.*;
import android.view.View.*;
import android.os.*;
import android.os.Process;
//this is an example of how to take a screenshot in a background service
//not very elegant, but it works (for me anyway)
public class ScreenshotService extends Service {
private Looper mServiceLooper;
private ServiceHandler mServiceHandler;
private Message msg;
private WebView webview;
// Handler that receives messages from the thread
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
webview = new WebView(ScreenshotService.this);
//without this toast message, screenshot will be blank, dont ask me why...
Toast.makeText(ScreenshotService.this, "Taking screenshot...", Toast.LENGTH_SHORT).show();
// This is the important code :)
webview.setDrawingCacheEnabled(true);
//width x height of your webview and the resulting screenshot
webview.measure(600, 400);
webview.layout(0, 0, 600, 400);
webview.loadUrl("http://stackoverflow.com");
webview.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
//without this method, your app may crash...
}
@Override
public void onPageFinished(WebView view, String url) {
new takeScreenshotTask().execute();
stopSelf();
}
});
}
}
private class takeScreenshotTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void[] p1) {
//allow the webview to render
synchronized (this) {try {wait(350);} catch (InterruptedException e) {}}
//here I save the bitmap to file
Bitmap b = webview.getDrawingCache();
File file = new File("/sdcard/example-screenshot.png");
OutputStream out;
try {
out = new BufferedOutputStream(new FileOutputStream(file));
b.compress(Bitmap.CompressFormat.PNG, 100, out);
out.close();
} catch (IOException e) {
Log.e("ScreenshotService", "IOException while trying to save thumbnail, Is /sdcard/ writable?");
e.printStackTrace();
}
Toast.makeText(ScreenshotService.this, "Screenshot taken", Toast.LENGTH_SHORT).show();
return null;
}
}
//service related stuff below, its probably easyer to use intentService...
@Override
public void onCreate() {
HandlerThread thread = new HandlerThread("ServiceStartArguments", Process.THREAD_PRIORITY_BACKGROUND);
thread.start();
// Get the HandlerThread's Looper and use it for our Handler
mServiceLooper = thread.getLooper();
mServiceHandler = new ServiceHandler(mServiceLooper);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// For each start request, send a message to start a job and deliver the
// start ID so we know which request we're stopping when we finish the job
msg = mServiceHandler.obtainMessage();
msg.arg1 = startId;
mServiceHandler.sendMessage(msg);
// If we get killed, after returning from here, restart
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// We don't provide binding, so return null
return null;
}
@Override
public void onDestroy() {
}
}
这篇关于Android的:取一个网页的“屏幕截图”从后台服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!