Validating and reading a Word file in Android

前端 未结 6 1760
忘了有多久
忘了有多久 2020-12-05 16:19

I want to be able to access a word file in the sdcard of the user\'s phone, and the file will be chosen by the user. I want to be able to process the file, i.e. read its con

相关标签:
6条回答
  • 2020-12-05 16:43

    I am not sure but what I understand is that what you want is to be able to be sure a file is a word document without simply looking at the extension.

    I think word documents have a file signature (see this link)

    The file signature is in that case a OLE Compound File format having the following binary in header position

    d0 cf 11 e0 a1 b1 1a e1

    So checking the corresponding bytes should say if it is a word document or not. Be careful, I am not sure if the binary is specific for word, doc or docx or available to all microsoft office : powerpoint, excel, word.

    You should perhaps search around the web for "microsoft word document file signature". This will certainly give you the information you are looking for.

    Now, how to "edit" it is another question.

    0 讨论(0)
  • 2020-12-05 16:46

    Simplest way IMHO is to port Apache POI library to Android. It's possible and there proof of that

    Porting POI you can embed Word editing feature into your application.

    Piece of code like:

    Intent intent = new Intent(Intent.ACTION_EDIT); 
    Uri uri = Uri.parse("file:///"+file.getAbsolutePath()); 
    intent.setDataAndType(uri, "plain/text"); 
    startActivity(intent);
    

    Simply means that Word file will be edited by some other application - not yours. As far as I understand it's not exactly what you want.

    0 讨论(0)
  • 2020-12-05 16:49

    If you want to edit the data directly, just do an intent to an available text editor.

    Intent intent = new Intent(Intent.ACTION_EDIT); 
    Uri uri = Uri.parse("file:///"+file.getAbsolutePath()); 
    intent.setDataAndType(uri, "plain/text"); 
    startActivity(intent);
    
    0 讨论(0)
  • 2020-12-05 16:50

    Reading and writing to a Word document

    Porting Apache POI to Android is most likely the only realistic solution for a project with man-hour constraints. It has been done by others and there is some information available on the web how to do it. The following blog has an example for Excel, which you can take as a starting point for a Word solution:

    Android read write excel file using Apache POI

    Validation

    While Apache POI is most likely your best option, some of the documents it opens will not be possible to open in MS Word, and consequently are not valid MS Word documents.

    The Word format's structure is quite complex. It's not simply a stream of text with tags. It is a proprietary format, and it's impossible to validate a Word file with complete accuracy without access to the format structure.

    In the end, the only 100% accurate validatation of a Word document is if it opens in MS Word. This discussion with Jay Freedman, Microsoft Word MVP, provides some perspective on the validation question and some insight into the Word format's structure.

    Net, if you use Apache POI, you will have some false positives and it depends on your customer's requirements whether this is acceptable or not.

    0 讨论(0)
  • 2020-12-05 16:52

    so, as i write above. The simpliest way to check if it's word-file (.doc) is to get it's absolute path and check it with path.endsWith(".doc");

    Android doesn't have default app to view .doc-files, but if user has installed such an app (i.e. Polaris Office), you can view it in this way:

    //Uri uri = Uri.parse("file://"+file.getAbsolutePath());
    Intent intent = new Intent();
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setAction(Intent.ACTION_VIEW);
    String type = "application/msword";
    intent.setDataAndType(Uri.fromFile(file), type);
    startActivity(intent);  
    
    0 讨论(0)
  • 2020-12-05 17:05

    Your question needs some more clarification

    Anyway this is what you should do to edit the file

    1. Read the file

      File sdcard = Environment.getExternalStorageDirectory();
      File file = new File(sdcard,"theFILE");
      StringBuilder content = new StringBuilder();
      try {
          BufferedReader br = new BufferedReader(new FileReader(file));
          String line;
          while ((line = br.readLine()) != null) {
              content.append(line);
              content.append('\n');
          }
      }
      catch (IOException e) {
      }
      
    2. Edit the content

      content.append("bla bla");
      content.append("kaza maza");
      
    3. Save the file

      FileOutputStream f = new FileOutputStream(file);
      f.write(content.toString().toCharArray());
      

    Remember to add to your manifest the permissions to write to the sdcard:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    Validation

    Unfortunately this is the hardest part. Apache POI is a choice; however it is a large library that does not guarantee the right behavior.

    The perfect solution is to check the MS-DOC format. It is a really big requirement. For such a limited application, I guess you won't find anything if you search the web. Therefore, you will have to implement a reader that abides by this format.

    0 讨论(0)
提交回复
热议问题