Oct 29 | 8:25 AM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Oct 29 | 8:40 AM |
Kai | has entered the room |
Kai |
Hello
|
Mark M. |
hello, Kai!
|
Mark M. |
how can I help you today?
|
Kai |
I thought I had another hour :D
|
Kai |
By answering some questions about scoped storage X)
|
Mark M. |
oboy
|
Kai |
Yes
|
Kai |
I have a docx in my app that I want to open with word, edit it, then close word and come back to the app.
|
Mark M. |
how are you defining "in my app" in the phrase "I have a docx in my app"? do you mean it is packaged as an asset?
|
Kai |
Unfortunately Word App doesn't wanna write on documents that aren't in public storage.
|
Kai |
It's a in my private storage.
|
Mark M. |
OK, so getFilesDir() or something like that?
|
Kai |
Yes.
|
Mark M. |
so, you're using FileProvider to make it available to the Word app?
|
Oct 29 | 8:45 AM |
Kai |
What I did before Android 11 was put it in Documents, wait for MediaStore to pick it up, make it available via a FileProvider and do startActivityForResult. After coming back I'd just read it and delete it.
|
Kai |
Now with Android 11... &=?"§%)!
|
Mark M. |
but... from Word's standpoint, FileProvider for something in external storage and FileProvider for something internal storage is indistinguishable
|
Kai |
Apparently not.
|
Mark M. |
(er, "something in internal storage")
|
Kai |
Or I get something wrong. https://github.com/cryptomator/cryptomator-andr...
|
Kai |
"Microsoft apps have currently problems to access files shared to them with write permissions in internal storage."
|
Kai |
Just checking if I really use FileProvider, but I think I do.
|
Kai |
It seems to be without FileProvider. Just a "ACTION_EDIT" Intent.
|
Oct 29 | 8:50 AM |
Mark M. |
where is the Uri coming from that you are putting in that Intent?
|
Kai |
MediaScannerConnection.scanFile on the file path.
|
Kai |
No, sorry.
|
Kai |
So far it was assembled "by hand".
|
Mark M. |
um, that's almost always the wrong answer
|
Mark M. |
(and not just an Android-10-and-above wrong answer)
|
Mark M. |
so... how are you assembling this Uri? what scheme does it have?
|
Kai |
I actually don't know
|
Kai |
There is utility functions and a Wrapper for file system access.
|
Oct 29 | 8:55 AM |
Mark M. |
if you log the Uri, what does it look like?
|
Oct 29 | 9:00 AM |
Kai |
/storage/emulated/0/Documents/com.ourcompany.android.v20/(866E3BB1-8D8B-6F3F-1DF7-66EB74173DE5)/(476AB95C-4D19-5B71-03BC-4744C34293F8).docx
|
Mark M. |
that is a path, not a Uri -- a Uri has a scheme
|
Kai |
That's not it.
|
Kai | |
Mark M. |
OK, so that's a Uri from the MediaStore
|
Mark M. |
MediaStore AFAIK on Android 10+ does not give you any more rights than you would have to the content on the filesystem
|
Kai |
The strange thing is that I can write content via MediaStore, but later I won't be able to read it.
|
Mark M. |
yeah, Downloads is weird
|
Mark M. |
I mean, even using "scoped storage is perfectly normal" as a baseline, Downloads is weird
|
Kai |
And I don't wanna use Documents as that is undocumented.
|
Mark M. |
if you start with "scoped storage is weird", Downloads is something straight out of a horror film
|
Kai |
And could go away
|
Kai |
I tried picking a Folder in Documents via SAF to have access, but opening it later via "File" failed.
|
Oct 29 | 9:05 AM |
Mark M. |
I'm not certain what "opening it... via "File"" means, exactly -- if you are trying to use filesystem APIs to work with a SAF document, though, that's not gonna work
|
Mark M. |
particularly since you are not planning on keeping the file around (delete once Word is done), my inclination would be for you to download the file to a filesystem location that is yours (e.g., getCacheDir()) and use FileProvider to make it available to Word
|
Kai |
But then Word won't write on it.
|
Mark M. |
well, that depends a bit on what Word is doing
|
Kai |
That's the whole problem pretty much. That the file has to be in public storage.
|
Mark M. |
if you include both FLAG_GRANT_READ_URI_PERMISSION and FLAG_GRANT_WRITE_URI_PERMISSION on the Uri, Word should have rights to write to it
|
Kai |
It DOES have rights to do so, it just doesn't use it
|
Mark M. |
going back to the GitHub issue that you linked to, I can see two main possibilities
|
Kai |
That's the whole reason for jumping through hoops even before Android 11, copying that file around.
|
Mark M. |
the "MS engineers are idiots" possibility: they are examining the Uri, trying to extract a path from it, convert that to a filesystem path, and use that
|
Mark M. |
the "the app has a bug" possibility: they did not have FLAG_GRANT_WRITE_URI_PERMISSION on the one Uri, and Word tries a benign write to the Uri to test write access
|
Mark M. |
when I said that there's no difference from Word's standpoint with FileProvider of whether the file is on internal or external storage, I'm quite serious
|
Oct 29 | 9:10 AM |
Mark M. |
it's akin to saying "well, Chrome can access Web pages on this partition on the Web server but not that partition" -- at most, that's a Web server problem, not a Chrome problem
|
Kai |
I tried sharing with a file provider too, from internal storage. It doesn't work.
|
Mark M. |
IMHO, there either was a bug in your test, or MS engineers are idiots
|
Mark M. |
in the latter case, you're seriously screwed
|
Kai |
I did a minimal working example with the generic FileProvider, FLAG_GRANT_* Permissions and ACTION_EDIT
|
Kai |
I think there is a third possibility: They don't want other apps to be able to use Word "just as an editor". They wan't to get control of the files, that Word etc. own the files that are edited.
|
Mark M. |
and Word read the content but treated it as read-only?
|
Kai |
Yes
|
Kai |
Exactly.
|
Mark M. |
that third possibility does not line up with the GitHub issue that you linked to
|
Kai |
Why not? "Only if the files are shared from public storage, they are able to open it with write access."
|
Kai |
Oh sorry, yes.
|
Mark M. |
in that issue, FileProvider *does* work, for certain files
|
Mark M. |
so, if we assume that issue is accurate, and we assume that you didn't have a bug in your test, then MS engineers are idiots
|
Kai |
I guess so
|
Mark M. |
they are trying to bypass FileProvider and work directly with the underlying file
|
Mark M. |
which only works if they can suss out the filesystem path based on the Uri and they have access to that file
|
Kai |
Question is what how I can work around it, given Android 11.
|
Mark M. |
as I wrote, you're seriously screwed
|
Kai |
Is there no way to write a random document to a public place, share it and then read it again in Android 11?
|
Oct 29 | 9:15 AM |
Mark M. |
not using java.io.File in both apps
|
Mark M. |
I mean, that's why we have FileProvider
|
Kai |
I'm open to using other means in my app
|
Mark M. |
well, you tried the other means (FileProvider), and Word does not want to play ball
|
Mark M. |
for the limited case of certain types of media (images, videos, audio), there are more options
|
Kai |
The main problem is that I can't read the file after word is done.
|
Mark M. |
right, because Downloads is basically write-only, screwy as that sounds
|
Kai |
w
|
Kai |
t
|
Kai |
f
|
Mark M. |
I'm actually surprised Word can use something from there
|
Kai |
So with Android 11 our customers just won't be able to edit word documents anymore right from the app.
|
Kai |
Good job, Google.
|
Kai |
And Microsoft.
|
Mark M. |
it's more that I don't have a for-certain solution for you
|
Kai |
I am still trying but more and more losing hope to finding one.
|
Kai |
There just might not be one
|
Mark M. |
in principle, if you used ACTION_OPEN_DOCUMENT, let the user decide where to put the content, took persistent permissions on the Uri you get back, and work with that Uri, you should be able to use FLAG_GRANT_* with that Uri to let Word access the same content
|
Mark M. |
however, I'm not 100% certain of that
|
Kai |
That would be better than nothing.
|
Kai |
Even though it introduces another step
|
Mark M. |
and, given that this is a DOCX file, I'm not certain if whatever code you're using to manipulate it on your side will handle an InputStream as a data source
|
Oct 29 | 9:20 AM |
Kai |
I was hoping that I could use SAF to get access to a directory somewhere and then use the File API to access that at will, as an "exchange directory". But as you said, Downloads is kinda "write only" apparently. And Documents seems to be strange too.
|
Kai |
It does handle an InputStream as a data source.
|
Mark M. |
"then use the File API to access that at will" -- nope, no more than you get a URL from a Web server then try using local files to work with stuff on the Web server
|
Mark M. |
if your DOCX-capable code can read and write using InputStream and OutputStream, then ACTION_OPEN_DOCUMENT or ACTION_OPEN_DOCUMENT_TREE should be possibilities
|
Kai |
Why not? The File API is just an abstraction of MediaStore and I was hoping it would also remember and use the same rights.
|
Mark M. |
yes, but ACTION_OPEN_DOCUMENT has nothing to do with MediaStore
|
Kai |
I was using ACTION_OPEN_DOCUMENT_TREE and took the permissions
|
Kai |
And was hoping it would somehow use that.
|
Mark M. |
use DocumentFile.fromTreeUri() to get a DocumentFile for the tree that you opened
|
Mark M. |
then use createNewFile() on that tree DocumentFile to get a DocumentFile on a new document inside of that tree
|
Kai |
So I cannot use SAF and the File API together?
|
Mark M. |
if by File API you mean java.io.File, then no
|
Mark M. |
however, given a DocumentFile on the new file, you can use getUri() to get the Uri for that document
|
Mark M. |
and pass that to openInputStream() and openOutputStream() on a ContentResolver
|
Mark M. |
that will give you the ability to read and write content in that document
|
Oct 29 | 9:25 AM |
Mark M. |
you do not need java.io.File for that, which is good, since that's not an option
|
Kai |
Ok
|
Mark M. |
so, you use openOutputStream() as the target for your download operation
|
Mark M. |
you use the Uri to the document with FLAG_GRANT_* and ACTION_EDIT and see if Word plays ball
|
Mark M. |
my guess is: Word will not play ball
|
Mark M. |
which is why I'm saying that you're all kinds of screwed
|
Kai |
It's another thing to try before throwing the towel, I guess.
|
Mark M. |
the problem isn't that you cannot provide a writable Uri to Word -- it's that Word doesn't appear to want to use those, if they have a content scheme
|
Mark M. |
but, yes, it's worth a try
|
Mark M. |
perhaps part of their "can we use this Uri" algorithm handles SAF Uri values better
|
Mark M. |
in the end, MS needed to do a better job of supporting content Uri values
|
Kai |
Yes.
|
Kai |
Very much so
|
Mark M. |
however, until Google started tightening the screws, there was nothing forcing MS to do that sort of thing
|
Mark M. |
eventually, MS will probably "straighten up and fly right"
|
Mark M. |
in the interim, trying SAF is worth a shot, but I would not be surprised if it fails for the same reason your FileProvider test fails
|
Kai |
If I can read the file afterwards, that would be progress
|
Kai |
And almost everything I want (besides the user having to pick a file destination).
|
Mark M. |
with the SAF option, reading the content afterwards shouldn't be a problem
|
Oct 29 | 9:30 AM |
Kai |
I'll let you know on saturday ;-)
|
Mark M. |
whether Word wants to write to the Uri is the big question
|
Mark M. |
OK, sounds good
|
Mark M. |
and that's a wrap for today's chat -- next one is Saturday at 4pm US Eastern
|
Kai |
Stay healthy and have a good one
|
Kai |
And thanks
|
Mark M. |
have a pleasant day, and best of luck with your SAF experiment!
|
Kai |
:)
|
Kai | has left the room |
Mark M. | turned off guest access |