android如何调取网站后台的图片,Android:从后台服务获取网页的“屏幕截图”?...

弄清楚,我必须设置WebView的“大小”,以便生成的“屏幕截图”不是0 x 0大小.然后我必须直接从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 {

@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() {

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值