Convert a pdf to String in android
<p>I was using the following function to convert a pdf to string:</p>

<pre><code>        private String GetString(String filepath) throws IOException {
        InputStream inputStream = new FileInputStream(filepath);
        String inputStreamToString = inputStream.toString();
        byte[] byteArray = inputStreamToString.getBytes();
        String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
        return encoded;
    }
</code></pre>

<p>I got the output as:</p>

<pre><code>amF2YS5pby5GaWxlSW5wdXRTdHJlYW1ANTM1MDhmNTg=
</code></pre>

<p>I have found that this output is wrong. Because certainly when I encode a 2MB pdf file, it can't be so short. Actually I base64 decoded in php server and the output was an invalid pdf. So my question is what is missing in the function?  </p>

<p>Ok. Problem solved. The following is the correct code to do this:</p>

<pre><code>private String GetString(String filepath) throws IOException {
            InputStream inputStream = new FileInputStream(filepath);
            byte[] byteArray = IOUtils.toByteArray(inputStream);
            String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT);
            return encoded;
        }
</code></pre>