Mar*_*sch 26 android screenshot webview
我认为标题几乎涵盖了它,但我的活动中有一个webview.我已经在网页视图中加载了一个网址,我想要整个页面的截图(视口中的内容以及"下方"的内容).
我有代码可以对视口进行快照,我知道这可以通过在快照之前放大webview来在iOS中完成.我试过在这里使用相同的技术:
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);
}
Run Code Online (Sandbox Code Playgroud)
但我仍然只捕捉视口.由于页面太大,忽略了内存不足等问题,是否有人知道我做错了什么或者如何在视口外包含部分?
avi*_*ash 25
试试这个吧
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;
}
}
Run Code Online (Sandbox Code Playgroud)
在AndroidManifest.xml文件中添加INTERNET PERMISSION和WRITE_EXTERNAL_STORAGE.
如果应用程序在Marshmallow上或上方运行,则需要在运行时请求权限以进行文件写入.
webview.capturePicture() 已弃用,使用以下方式可以:
Bitmap bitmap = Bitmap.createBitmap(webview.getWidth(), webview.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
webview.draw(canvas);
return bitmap;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24112 次 |
| 最近记录: |