如何验证android内容provider/resolver文件保存成功?

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

在我的应用程序中,我可以读取文件并将其保存到内容提供商。

调用此代码时,如果没有抛出异常,我认为文件已正确保存。

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

但是在使用 google 驱动器时我发现这可能不是真的,例如如果用户没有互联网连接,文件的上传就会暂停。

我如何知道内容提供者是否没有将更改应用到文件?
有没有办法在我知道初始保存失败后重试上传?

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

这是不可能的,抱歉。

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 驱动器最终应该会上传内容。如果不是,那是 Google 驱动器中的错误。您将内容交给了那个应用程序;使用该内容为用户做一些有用的事情是该应用程序的责任。

但是,“文件上传暂停”的概念是 Google Drive 的内部实现细节。并非每个 ContentProvider 都执行上传。 Google 云端硬盘可能出于多种原因不一定会立即上传某些内容,而不仅仅是与 Internet 连接相关(例如,先执行一些转换)。不立即上传内容不一定是错误,并且没有 API 表示“嘿,你做了这件事了吗?”,当有无数种可能的事情可以完成时。

不可能知道内容提供者对文件应用了更改