0

I am trying to get the docx file as shown below

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == MainActivity.RESULT_OK) {

        if (resultCode == RESULT_OK) {
            // Get the Uri of the selected file
            Uri uri = data.getData();
            String uriString = uri.toString();
            File myFile = new File(uriString);
            String path = myFile.getAbsolutePath();
            filepath =path;
            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));
                        txtFilename.setText(displayName);
                        filename = displayName;

                    }
                } finally {
                    cursor.close();
                }
            } else if (uriString.startsWith("file://")) {
                displayName = myFile.getName();
                txtFilename.setText(displayName);
                filename = displayName;

            }
        }
    }
    }

Now the result is :

Path: /content:/com.estrongs.files/storage/emulated/0/Download/Imp%20Values.docx

Filename: Imp Values.docx

Now how do I convert this document to base64 ?

Thanks in advance.

coder
  • 13,002
  • 31
  • 112
  • 214
  • I have never tried it, but I think You could convert Your doc to byte[] like this: http://stackoverflow.com/questions/6058003/elegant-way-to-read-file-into-byte-array-in-java and then convert this byte[] into Base64 with Base64.encodeToString(yourByteArray,Base64.DEFAULT). But I read something that this will grow up Your file size.... – Opiatefuchs Mar 08 '16 at 11:26
  • 1
    for my understanding, it must be like converting an image into Base64..... – Opiatefuchs Mar 08 '16 at 11:27

2 Answers2

5

Try this, Hope it will work, I did for .pdf and .doc

public static String convertFileToByteArray(File f) {
    byte[] byteArray = null;
    try {
        InputStream inputStream = new FileInputStream(f);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] b = new byte[1024 * 11];
        int bytesRead = 0;

        while ((bytesRead = inputStream.read(b)) != -1) {
            bos.write(b, 0, bytesRead);
        }

        byteArray = bos.toByteArray();

        Log.e("Byte array", ">" + byteArray);

    } catch (IOException e) {
        e.printStackTrace();
    }
    return Base64.encodeToString(byteArray, Base64.NO_WRAP);
}

For picking file :

private void chooseFile() {
    try {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("*/*");
        try {
            startActivityForResult(intent, LOAD_IMAGE_RESULTS);

        } catch (ActivityNotFoundException e) {

        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}
Nisarg
  • 1,358
  • 14
  • 30
0

The above code always returns a nullpointerexception but this solved it without any error

// Converting File to Base64.encode String type using Method
    public String getStringFile(File f) {
        InputStream inputStream = null; 
        String encodedFile= "", lastVal;
        try {
            inputStream = new FileInputStream(f.getAbsolutePath());

        byte[] buffer = new byte[10240];//specify the size to allow
        int bytesRead;
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        Base64OutputStream output64 = new Base64OutputStream(output, Base64.DEFAULT);

            while ((bytesRead = inputStream.read(buffer)) != -1) {
                output64.write(buffer, 0, bytesRead);
            }
        output64.close();
        encodedFile =  output.toString();
        } 
         catch (FileNotFoundException e1 ) {
                e1.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        lastVal = encodedFile;
        return lastVal;
    }
Edwinfad
  • 515
  • 6
  • 14
  • this trows `java.lang.OutOfMemoryError: Failed to allocate a 453230608 byte allocation with 6291456 free bytes and 165MB until OOM, max allowed footprint 235231304, growth limit 402653184` at line `output64.write(buffer, 0, bytesRead)` – Ravi Vaniya May 14 '19 at 05:55