10

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 contents and modify it as the user wishes, and save it when the user clicks “save.”

Is it possible in Android? How can I make the program understand that the file is a Word file?

Note that I don't want to view the Word file. The user will select the Word file and my app should be able to make some changes to it and save it.

jstrieb
  • 599
  • 1
  • 7
  • 20
Ashwin
  • 12,691
  • 31
  • 118
  • 190
  • Is user enable to chose only word-files? Anyway you can get AbsolutePath of this file and check it as `path.endsWith(".doc");` – user1049280 May 11 '12 at 09:10
  • @user1049280 : you cannot be sure that everything that ends with '.doc' is a word file. What if I store an image with an extension 'image.doc' . – Ashwin May 11 '12 at 09:17
  • 1
    you know, in OS like Windows it's only user's headache that he has saved something with incorrect extension :) – user1049280 May 11 '12 at 09:21
  • @user1049280 : Actually this is a security app. So validating the extension is a must:) – Ashwin May 11 '12 at 09:25
  • Until you specify exactly what you want to do to the file, this question is too broad to be answered. At best people could suggest libraries for handling or converting word files, or extracting funcionality from something like open office and porting it. – Chris Stratton May 13 '12 at 18:36
  • @ChrisStratton : I want ot be able to change this file's contents programmatically. for example I want to count the number of lines in the word file and then append this value at the end of the file and save it. I don't in anyway want to view the word file. First validate the word file and then modify its content a bit as explained above. – Ashwin May 14 '12 at 06:46

6 Answers6

2

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.

Community
  • 1
  • 1
Barmaley
  • 16,638
  • 18
  • 73
  • 146
  • I don't want to open any editor for editing the word file. I just want to add a fixed content at the end of the word file after reading its existing content. I want this to be done programmatically and then save the word file. – Ashwin May 14 '12 at 05:02
  • You will probably need to figure out how to use the file access functions of something like this directly, rather than to power an editor. – Chris Stratton May 14 '12 at 13:27
  • @Ashwin exactly the same you can do using POI library - this stuff fits perfect to your task :) – Barmaley May 14 '12 at 15:31
  • @barmaley : I will try it out and let you know – Ashwin May 15 '12 at 00:33
  • How to call it from Fragment? Please Help... @barmaley – Pratik Butani Nov 11 '13 at 10:15
2

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.

Gunnar Karlsson
  • 28,350
  • 10
  • 68
  • 71
1

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);  
user1049280
  • 5,176
  • 8
  • 35
  • 52
1

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.

HpTerm
  • 8,151
  • 12
  • 51
  • 67
1

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.

Sherif elKhatib
  • 45,786
  • 16
  • 89
  • 106
0

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);
  • @Ashwin It will open the file as plaintext i.e. What happens when you try to open a .doc in notepad. I don't know if you will be able to edit the actual text of the document without breaking the whole thing. – Martin Kemp May 11 '12 at 10:30
  • 1
    I don't want to view the contents. Once I create an input stream from that file. I want to validate whether it is a word doc. And if it is, I want to just modify the document pro grammatically and save it again. – Ashwin May 11 '12 at 10:53
  • @Ashwin I just checked, word/styles.xml word/fontTable.xml appears in .docx files, maybe searching for these strings specifically in the background can detemine if it is a real file? – Martin Kemp May 11 '12 at 11:23