Lobstw
Lobstw

Reputation: 499

Uri points to deleted file but still displays in app, how?

From inside my app, I take a picture using my camera. This picture is then loaded into an ImageView using its Uri which would look something like this: content://media/external/images/media/12345. I can get the path of this Uri by querying the MediaStore, it looks something like this: /storage/emulated/0/DCIM/Camera/IMG_12345.jpg.

I now manually delete the image from that path above. But when I reopen the app, the image is still there and accessible under the same Uri. If I query the MediaStore again it gives me a CursorIndexOutOfBoundsException. So the file does not exist to the MediaStore

So what's happening here? If I've deleted the file manually, then it should not be available to the app. Where is this file right now? Is there a better way of deleting images such that they are removed from everywhere?

Upvotes: 0

Views: 494

Answers (2)

CommonsWare
CommonsWare

Reputation: 1007533

The MediaStore is an index of available files. Like any index, it needs to be updated when there are changes to the filesystem. And, like a search engine, there are two main ways in which that happens:

  • Somebody tells the MediaStore to scan something and add it to the index
  • The MediaStore crawls the fileystem

In your case, deleting the file using a file manager may not have updated MediaStore, in part because there is no documentation on how to update the MediaStore when you delete something. The docs are focused on indexing new files, not de-listing deleted ones.

The reason why the Uri appeared to continue to work is because of your image-loading library (Glide, in your case). Glide has an in-memory cache, and it will use images from cache when possible.

Upvotes: 1

Psi
Psi

Reputation: 6793

You should read about caching: https://en.wikipedia.org/wiki/Web_cache

When downloading a resource (such as an image) for the first time, the browser (or most apps using a built-in http communication component) save that resource in the so-called cache. When it comes to reading the resource again next time, they find it has been cached locally, so they don't need to load it again over the net.

You don't have full control over the behaviour, but you can serve your resource via HTTP using the Cache-control and Proxy headers: https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

If you want to remove that resource from the cache, well, you have to clean your browser cache. Usually, this can be done in the system settings or in the settings of your browser.

Upvotes: 0

Related Questions