Generate bitmap from HTML in Android

后端 未结 5 1754
别跟我提以往
别跟我提以往 2020-11-30 03:25

How do can you generate a bitmap from HTML in Android?

Can the WebView be used for this or is there a better approach (like maybe using the WebVie

5条回答
  •  离开以前
    2020-11-30 04:07

    This example shows how to capture webView content last picture (it waits until webview complete rendering picture), it is an example of convert HTML to PNG using Android

    Activity Code

    public class HtmlViewer extends Activity {
    private String HTML; 
    private Context ctx;
    private Picture pic = null;
    private int i=0; suppose this is the last pic
    private int oldi = 0; 
    private Timer myTimer; // timer for waiting until last picture loaded
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_html_viewer);
        Intent intent = getIntent();
        HTML = intent.getStringExtra("HTML");
        ctx = this;
        WebView wv = (WebView)findViewById(R.id.webView1);
        wv.setPictureListener(new PictureListener(){
    
            public void onNewPicture(WebView view, Picture picture) {
                Log.w("picture", "loading.." + String.valueOf(view.getProgress()));
                pic = picture;
                i++;
    
    
                }
    
            });
        wv.loadData(HTML, "text/html; charset=utf-8", null);
    
        wv.setWebViewClient(new WebViewClient()     
        {
            public void onPageFinished(WebView wv, String url)
            {
                Picture p = wv.capturePicture();
                myTimer = new Timer();
                myTimer.schedule(new TimerTask() {          
                    @Override
                    public void run() {
                        if (i > oldi)
                            oldi = i;
                            else
                                if (i != 0)
                            {
                                Log.w("picture", "finished");
                                cancel();
                                Picture picture = pic;
    
                                Log.w("picture", "onNewPicture- Height"+ picture.getHeight());
                                Log.w("picture", "onNewPicture- Width"+ picture.getWidth());
    
                                File sdCard = Environment.getExternalStorageDirectory();
                                if (picture != null)
                                {
                                    Log.w("picture", " P OK");
                                Bitmap image = Bitmap.createBitmap(picture.getWidth(),picture.getHeight(), Config.ARGB_8888);
                                Canvas canvas = new Canvas(image);
                                picture.draw(canvas);
                                Log.w("picture", "C OK");
    
                                if (image != null) {
                                    Log.w("picture", "I OK");
                                    ByteArrayOutputStream mByteArrayOS = new ByteArrayOutputStream();
                                    image.compress(Bitmap.CompressFormat.PNG, 90, mByteArrayOS);
                                    try {
                                        File file = new File(sdCard, "AccountView.PNG");
                                        FileOutputStream fos = new FileOutputStream(file);
                                        fos.write(mByteArrayOS.toByteArray());
                                        fos.flush();
                                        fos.close();                                        
                                        Log.w("picture", "F OK " + String.valueOf(mByteArrayOS.size()) + " ? " + String.valueOf(file.length()));
                                        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                                        Uri screenshotUri = Uri.fromFile(file);                      
                                        sharingIntent.setType("image/png");
                                        sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
                                        startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.ACCOUNT_VIEW_TITLE)));                      
                                        ((Activity)ctx).finish();
                                    } catch (FileNotFoundException e) {
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }               
                                }
    
                            }
                    }
    
                }, 0, 1000);
    
                Log.w("picture", "done");
    
                loadcompleted = true;
            }
        });
    
    
    
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_html_viewer, menu);
        return true;
    }
    
    
    
    }
    

    Layout

    
    
    
    

提交回复
热议问题