title: How to convert Uri image into Base64?
body: <p>I have write code to convert image from file location into base64. I can easily convert image into base64 from absolute file location like: <strong>C:/Users/Java Engineer/Desktop/test/gallery/magar/Kanuglam.jpg</strong> , but I can not convert from location like<br>
  <a href="https://i.stack.imgur.com/BbYgv.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BbYgv.jpg" alt="enter image description here"></a>
  . I want to convert image to use in android from web-service.
  Here is code sample :</p>
  
  <pre><code>/**
   * TEST JSON
   */
  String convertToJsonArrayWithImageForMovieDetailTest(ResultSet rs) {
  
      System.out.println("I am insied json converter");
      JSONArray list = new JSONArray();
      JSONObject obj ;
  
      //File file;
      File locatedFile;
      FileInputStream fileInputStream;
      try {
  
          while (rs.next()) {
              obj = new JSONObject();
              System.out.println("inside RS");
              System.out.println("date is there ha ha ");
  
              obj.put("movie_name", rs.getString("name"));
              obj.put("movie_gener", rs.getString("type"));
              String is_free_stuff = rs.getString("is_free_stuff");
              if (is_free_stuff == "no") {
                  is_free_stuff = "PAID";
              } else {
                  is_free_stuff = "FREE";
              }
              obj.put("movie_type", is_free_stuff);
  
              //String movie_image = rs.getString("preview_image");
  
              //this does not work
              String movie_image = "http://www.hamropan.com/stores/slider/2016-09-10-852311027.jpg";
  
              //this works for me
          //  file = new File("C:/Users/Java Engineer/Desktop/Nike Zoom Basketball.jpg");
              locatedFile = new File(movie_image);
              // Reading a Image file from file system
              fileInputStream = new FileInputStream(locatedFile);
              if (locatedFile == null) {
                  obj.put("movie_image", "NULL");
              } else {
                  byte[] iarray = new byte[(int) locatedFile.length()];
                  fileInputStream.read(iarray);
                  byte[] img64 = com.sun.jersey.core.util.Base64
                          .encode(iarray);
                  String imageString = new String(img64);
                  obj.put("movie_image", imageString);
              }
              list.add(obj);
          }
  
  
      } catch (Exception e) {
          e.printStackTrace();
      }
      return list.toString();
  }
  </code></pre>
  
  <p><em>this block of code works for me but it seem slow</em></p>
  
  <pre><code>public String imageConvertMethod(String url) throws Exception{
      ByteArrayOutputStream output = new ByteArrayOutputStream();
  
      try (InputStream input = new URL(url).openStream()) {
          byte[] buffer = new byte[512];
          for (int length = 0; (length = input.read(buffer)) &gt; 0;) {
               output.write(buffer, 0, length);
          }
      }
  
      byte [] byte_array = output.toByteArray();
  
      byte[] img64 = com.sun.jersey.core.util.Base64
              .encode(byte_array);
      String  imageString = new String(img64);
  
      return imageString;
  }
  </code></pre>