owncloud / android

:phone: The ownCloud Android App
GNU General Public License v2.0
3.77k stars 3.05k forks source link

[BUG] Unable to get document correct URI with Intent.ACTION_CREATE_DOCUMENT and FLAG_GRANT_PERSISTABLE_URI_PERMISSION #3110

Open uazo opened 3 years ago

uazo commented 3 years ago

Actual behaviour

I am trying to use SAF to save a file directly in owncloud and the idea is to keep the URI returned so that I can eventually overwrite it. currently the code is

            Intent fileSelector = new Intent(Intent.ACTION_CREATE_DOCUMENT);
            fileSelector.addCategory(Intent.CATEGORY_OPENABLE);
            fileSelector.setType("text/*");
            fileSelector.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
                                  Intent.FLAG_GRANT_READ_URI_PERMISSION |
                                  Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
            fileSelector.putExtra(Intent.EXTRA_TITLE, "bookmarks.html");

upon receipt of the intent:

public void onIntentCompleted(WindowAndroid window, int resultCode, Intent data) {
            if (data == null) return;
            Uri filePath = data.getData();
            ContentResolver resolver = ContextUtils.getApplicationContext().getContentResolver();
            resolver.takePersistableUriPermission(filePath, Intent.FLAG_GRANT_WRITE_URI_PERMISSION |
                                                                        Intent.FLAG_GRANT_READ_URI_PERMISSION);
...

I receive as a filePath = content://org.owncloud.documents/document/-1. reselecting the same generated document instead returns a correct id

Expected behaviour

receive a value like content://org.owncloud.documents/document/<IDDOCUMENT>

Can this problem be reproduced with the official owncloud server? yes, actually I have content://org.owncloud.documents/document/-1

Environment data

Android version: A9

Device model: BLN-L22

Stock or customized system: Customized: Omni Rom

ownCloud app version: version 2.16 release 0259f7bef

ownCloud server version: ownCloud 10.4.1 (stable)

Thanks for your help

abelgardep commented 3 years ago

Hi @uazo, thanks for reporting it. You are right, we return a dummy id (-1) when creating a document. That's because that file won't be written till openDocument function is called. So we wait till we have an actual file to upload it to the server. But, considering your use case, we could try to improve this behavior.

uazo commented 3 years ago

thanks for the quick reply. if it can be useful for a quick fix, I seem to understand that other implementations (like drive) return a zero bytes file, which would be fine for me too.

That's because that file won't be written till openDocument function is called.

unfortunately I have not found any way to request an update of the URI: if there was a way that does not require the request for access to the entire folder it would also be fine.