我认为标题几乎涵盖了它,但我在我的活动中有一个webview。我已经在网页视图中加载了一个url,我想要截取整个页面的屏幕截图(无论是视口中的内容还是“折叠下”的内容)。
我已经有了截图视口的代码,我知道这可以在iOS中通过在截图之前放大webview来完成。我在这里尝试使用了相同的技术:
WebView browserView = (WebView) findViewById(R.id.browserView);
//Resize the webview to the height of the webpage
int pageHeight = browserView.getContentHeight();
LayoutParams browserParams = browserView.getLayoutParams();
browserView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, pageHeight));
//Capture the webview as a bitmap
browserView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(browserView.getDrawingCache());
browserView.setDrawingCacheEnabled(false);
//Create the filename to use
String randomFilenamepart = String.valueOf(new SecureRandom().nextInt(1000000));
String filename = Environment.getExternalStorageDirectory().toString() + "/Screenshot_" + randomFilenamepart + ".jpg";
File imageFile = new File(filename);
//Stream the file out to external storage as a JPEG
OutputStream fout = null;
try {
fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
browserView.setLayoutParams(browserParams);
}但我仍然只捕捉到了视口。抛开页面太大导致内存不足之类的事情不谈,有没有人知道我做错了什么,或者我如何才能包括视区之外的部分?
发布于 2013-01-07 19:38:19
试试这个
import java.io.FileOutputStream;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Picture;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
WebView w;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
w = new WebView(this);
w.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
Picture picture = view.capturePicture();
Bitmap b = Bitmap.createBitmap(picture.getWidth(),
picture.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
picture.draw(c);
FileOutputStream fos = null;
try {
fos = new FileOutputStream("mnt/sdcard/yahoo.jpg");
if (fos != null) {
b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.close();
}
} catch (Exception e) {
}
}
});
setContentView(w);
w.loadUrl("http://search.yahoo.com/search?p=android");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}在AndroidManifest.xml文件中添加上网权限和WRITE_EXTERNAL_STORAGE。
如果应用程序在棉花糖上或以上运行,则需要在运行时询问文件写入权限。
发布于 2019-05-15 10:28:07
webview.capturePicture()已弃用,使用下面的方法可以做到:
Bitmap bitmap = Bitmap.createBitmap(webview.getWidth(), webview.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
webview.draw(canvas);
return bitmap;发布于 2015-03-14 17:48:49
@avinash thakur代码实际上是有效的,你只是忘记在Android Manifest文件中添加<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>,否则,android不会允许你的流在设备上写入数据/文件。
干杯。
https://stackoverflow.com/questions/9745988
复制相似问题