Edgar Alvarado
Edgar Alvarado

Reputation: 87

How to validate android content provider/resolver succeeded in saving the file?

In my application I can read and save a file to a content provider.

When calling this code I assume the file was properly saved if no exceptions were thrown.

OutputStream outputStream = getContentResolver().openOutputStream(uri, "w");
// write to stream
outputStream.close();

But working with google drive I found out that this might not be true, for example if the user doesn't have an internet connection the file's upload is paused.

How can I know if the content provider hasn't applied the changes to the file?
Is there a way to retry uploading once I know the initial save failed?

Upvotes: 1

Views: 254

Answers (2)

Mohsen Mashkour
Mohsen Mashkour

Reputation: 70

it is impossible to know the content provider applied the changes to the file

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1007474

How can I know if the content provider hasn't applied the changes to the file?

That is not possible, sorry.

But working with google drive I found out that this might not be true, for example if the user doesn't have an internet connection the file's upload is paused.

Google Drive should eventually upload the content. If it does not, that is a bug in Google Drive. You handed off the content to that app; it is that app's responsibility to do something useful for the user with that content.

However, the concept of "file upload is paused" is an internal implementation detail of Google Drive. Not every ContentProvider performs uploads. And Google Drive might not necessarily immediately upload some content for any number of reasons, not just tied to Internet connectivity (e.g., perform some conversion first). Not immediately uploading content is not necessarily a bug, and there is no API for "hey, did you do the thing yet?", when there are an infinite number of possible things that could be done.

Upvotes: 1

Related Questions