title: Telegram Bot Java Library Downloaded Image Quality
body: <p>I am using <a href="https://github.com/rubenlagus/TelegramBots" rel="nofollow">TelegramBots </a> Java API by rubenlagus to develop my Telegram Bot. I am able to successfully send a photo to the bot and also retrieve the photo from Telegram as illustrated by <a href="https://github.com/rubenlagus/TelegramBots/blob/master/HOWTO.md#question_how_to_get_picture" rel="nofollow">this example</a> . Problem is that the image downloaded is smaller and also poor in quality as compared to actual image uploaded. Am storing the image as a blob using Java Persistence (JPA). Here's my code</p>
  
  <p>Sample Code That Receives Image from Client;</p>
  
  <pre><code>List&lt;PhotoSize&gt; photos = message.getPhoto();
              System.out.println("Photos --&gt; " + photos.size());
              for (int i = 0; i &lt; photos.size(); i++) {
  
                  GetFile getFileRequest = new GetFile();
  
                  getFileRequest.setFileId(photos.get(i).getFileId());
                  File file = getFile(getFileRequest);
                  //  System.out.println(file.getFilePath());
                  downloadFilePath = filePathUrl + file.getFilePath();
                  System.out.println("Photo --&gt; " + downloadFilePath);
                  java.io.File fileFromSystem =downloadFile(downloadFilePath);
  
                  byte[] bytes = new byte[(int) fileFromSystem.length()];
  
                  System.out.println( photo Size --&gt; " + bytes.length);
  
                  FileInputStream fileInputStream = new FileInputStream(fileFromSystem);
                  fileInputStream.read(bytes);
                  myEntity.setPhoto(bytes);
                  myFacade.edit(myEntity);
  </code></pre>
  
  <p>Download File Method;</p>
  
  <pre><code>private java.io.File downloadFile(String fileUrl) {
      java.io.File file = null;
      try {
  
          sysProps = System.getProperties();
          URL url = new URL(fileUrl);
          InputStream in = url.openStream();
          String directoryPath = sysProps.getProperty("file.separator") + sysProps.getProperty("user.home") + sysProps.getProperty("file.separator") + "Documents" + sysProps.getProperty("file.separator") + "dev";
          java.io.File directory = new java.io.File(directoryPath);
  
          String pathToFile = directoryPath + sysProps.getProperty("file.separator") + new Random().nextInt(100) + fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
  
          if (!directory.exists()) {
              directory.mkdirs();
          }
          file = new java.io.File(pathToFile);
          file.createNewFile();
  
          FileOutputStream outputStream = new FileOutputStream(file);
          int read = 0;
  
          byte[] bytes =  new byte[10000];
          while ((read = in.read(bytes)) != -1) {
              outputStream.write(bytes, 0, read);
  
          }
          outputStream.flush();
          outputStream.close();
  
      } catch (Exception ex) {
          ex.printStackTrace();
      }
      return file;
  }
  </code></pre>
  
  <p>Code That Sends Image To Client(Converts bytes[] from entity and sends to client)</p>
  
  <pre><code>  String strFilePath = sysProps.getProperty("user.home") + sysProps.getProperty("file.separator") + "Documents" + sysProps.getProperty("file.separator") + "dev" + sysProps.getProperty("file.separator") + new Random().nextInt(100) + ".jpeg";
                  FileOutputStream fos = new FileOutputStream(strFilePath);
                  fos.write(myEntity.getPhoto());
                  fos.close();
  
                  SendPhoto sendPhotoRequest = new SendPhoto();
                  sendPhotoRequest.setChatId(message.getChatId().toString());
                  java.io.File fileToSend = new java.io.File(strFilePath);
                  sendPhotoRequest.setNewPhoto(fileToSend);
  
                  //    System.out.println("Sending phtoto --&gt;   " + strFilePath );
                  sendPhoto(sendPhotoRequest);
                  fileToSend.delete();
  </code></pre>