11

I'm trying to load documents from files in my app using Microsoft Word and PDF viewers and I'm using a FileProvider to handle Android 7.0+ not allowing file URIs to be passed freely. I get the URI like so and and set the Intent flags to allow reading and writing before opening it, like so:

// From the byte array create a file containing that data, and get extension and MIME type.
                File fileToOpen = byteArrayToFile(documentData, shortFileName);
                String fileExtension = UtilityMethods.getFileExtension(fileToOpen.getName());
                String mime = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);

                // Get the Uri for the file from the file provider, open using intent.
                Uri documentUri = FileProvider.getUriForFile(getContext(), "com.mycompany.provider", fileToOpen);
                Intent intent = new Intent();
                intent.setDataAndType(documentUri, mime);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
                intent.setAction(Intent.ACTION_VIEW);
                startActivity(intent);

However when the file loads in MS Word the file is read only, and cannot be edited, which is not the desired behaviour. Where am I going wrong?

  • I'm not an expert, but looking at the code I'm gonna guess `intent.setAction(Intent.ACTION_VIEW)` perhaps be something like `ACTION_EDIT`? – James Oravec Oct 20 '16 at 14:34
  • Unfortunately setting the action to ACTION_EDIT instead of ACTION_VIEW produces the same wrong behaviour. The ACTION_VIEW action was in place when the code was simply getting the URI from the fromFile() method when reading and writing worked fine. – David Mcinnes Oct 20 '16 at 14:40
  • Try combining the two `addFlags()` calls into a single call. It's also possible that the problem is more in whatever you are using to load the content, as it may simply not support modification of content coming from a `content` `Uri` for some reason. – CommonsWare Oct 20 '16 at 14:43
  • I've changed the addFlags() call to 'intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);' and the behaviour is still the same unfortunately. My next test is to try other document applications to see whether the problem is with MS word. – David Mcinnes Oct 20 '16 at 15:15
  • 1
    The problem does indeed seem to be with the MS word android app as opposed to my code - GoogleDocs.apk will open the file using that code and allow write access. Annoying. – David Mcinnes Oct 20 '16 at 15:28
  • 3
    Having a similar error, any other app will work and open a file for editing, but MS Word (and other office apps by MS) seem to be unable of checking it has write permissions. Annoying indeed. – Jorge Aug 02 '17 at 06:36
  • 1
    Is Microsoft aware of that? How to 'open a ticket' on their side about that? – Pascal Jun 13 '18 at 03:51
  • 1
    any new solutions for this problem? Since, using any file manager i can open the word file with write permissions, using my app they are read only – Aiko West May 24 '19 at 08:06
  • As far as I am aware, no, this problem still affects my application. I am not sure how I would log this error. – David Mcinnes May 27 '19 at 07:49
  • 1
    Just wrote a technical documentation what I still found out: https://github.com/cryptomator/cryptomator-android/issues/150#issuecomment-514401775 Microsoft has to fix this problem as soon as possible! – Jules Jul 23 '19 at 22:12

0 Answers0