WebページのURLがあり、このWebページの「スクリーンショット」をバックグラウンドで撮りたいと考えています。サービス内で、ユーザーにUIを表示せずに。
サービスでWebViewを作成しようとしましたがcapturePicture()
、ページの読み込みが完了したときにこのメソッドを使用してスクリーンショットを取得しましたが、作成されたものPicture
(およびBitmap
そこから作成したもの)は常に空です。(これは通常のアクティビティでは完全に機能しますが、私のバックグラウンドサービスでは機能しません)。
これを機能させる方法、またはUIを使用せずにWebページの「スクリーンショット」を取得する別の方法はありますか?
注:この回答は古いです-私がこれを試した最新のAndroidバージョンは4.4、他のAndroidバージョンまたはこれをテストしなかったデバイスのYMMVです...これも非常に厄介なハックです-今日私はお勧めしますこのためにWebサービス/ APIを使用します。
それを理解したので、結果の「スクリーンショット」が0 x 0サイズにならないように、WebViewの「サイズ」を設定する必要があります。次に、ここでは機能していないように見えるため、WebViewの描画キャッシュからビットマップを直接取得するcapturePicture()
必要があります。
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() {
}
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加