convert pdf to base64 string java Android Code 
is working code 





Uri uri = data.getData();
String uriString = uri.toString();
File myFile = new File(uriString);
String path = myFile.getAbsolutePath();
String displayName = null;

if (uriString.startsWith("content://")) {
    Cursor cursor = null;
    try {
        cursor = this.getContentResolver().query(uri, null, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            inputStream = getContentResolver().openInputStream(uri);//You can get an inputStream using any IO API            byte[] bytes;
            byte[] buffer = new byte[8192];
            int bytesRead;
            ByteArrayOutputStream output = new ByteArrayOutputStream();
            try {
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    output.write(buffer, 0, bytesRead);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            bytes = output.toByteArray();
            String encodedString = Base64.encodeToString(bytes, Base64.DEFAULT);
            //Tst(CompleteRegistration.this, encodedString);            w9form = encodedString;
        }
    } finally {
        cursor.close();
    }

}

Comments

Popular posts from this blog