Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

java - When using Android file provider, files don't have correct permissions despite FLAG_GRANT_WRITE_URI_PERMISSION being flagged in intent

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...