Convert docx, doc to base64 android

前端 未结 2 716
温柔的废话
温柔的废话 2021-01-17 02:16

I am trying to get the docx file as shown below

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


        
2条回答
  •  梦毁少年i
    2021-01-17 02:40

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

提交回复
热议问题