【问题标题】:Convert docx, doc to base64 android将docx、doc转换为base64 android
【发布时间】:2016-06-22 07:22:16
【问题描述】:

我正在尝试获取如下所示的 docx 文件

 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;

            }
        }
    }
    }

现在结果是:

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

文件名:Imp Values.docx

现在如何将此文档转换为 base64?

提前致谢。

【问题讨论】:

  • 我从未尝试过,但我认为您可以像这样将您的文档转换为 byte[]:stackoverflow.com/questions/6058003/… 然后使用 Base64.encodeToString(yourByteArray,Base64.默认)。但我读到一些东西,这会增加你的文件大小....
  • 就我的理解来说,应该是把图片转成Base64吧.....

标签: android base64 document docx doc


【解决方案1】:

上面的代码总是返回一个空指针异常,但这解决了它没有任何错误

// 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;
    }

【讨论】:

  • 这在java.lang.OutOfMemoryError: Failed to allocate a 453230608 byte allocation with 6291456 free bytes and 165MB until OOM, max allowed footprint 235231304, growth limit 402653184output64.write(buffer, 0, bytesRead)
【解决方案2】:

试试这个,希望它会起作用,我为 .pdf 和 .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);
}

用于挑选文件:

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- 尝试获取空数组异常的长度
  • 请查看this
  • 现在将文件大小设为“0”
  • @Nisarg- 谢谢你的时间 :) 工作得很好
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-16
  • 2012-05-17
  • 2020-05-22
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多