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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…