How to convert a google charts graph to image in a

2019-02-28 10:40发布

I want to convert a line graph I made with google charts api to image.How should I do?I didn't found on the net anything that can help.

1条回答
够拽才男人
2楼-- · 2019-02-28 11:06

try to convert your google chart as bitmap and set as your image to your imageview.

for ex:

Bitmap bitmap = loadChart("your google image url");

and loadChart method is

private Bitmap loadChart(String urlRqs) {
        Bitmap bm = null;
        InputStream inputStream = null;

        try {
            inputStream = OpenHttpConnection(urlRqs);
            bm = BitmapFactory.decodeStream(inputStream);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return bm;
    }

and OpenHttpConnection is

private InputStream OpenHttpConnection(String strURL) throws IOException {

        InputStream is = null;
          URL url = new URL(strURL);
          URLConnection urlConnection = url.openConnection();

          try{
           HttpURLConnection httpConn = (HttpURLConnection)urlConnection;
           httpConn.setRequestMethod("GET");
           httpConn.connect();

           if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            is = httpConn.getInputStream(); 
           }
          }catch (Exception ex){
              Log.e("###","",ex);
          }

        return is;
    } 

And finally you can set your bitmap as your image view background by using following code.

image.setImageBitmap(bitmap);

Try it. I hope this will help you.

查看更多
登录 后发表回答