bumptech / glide

An image loading and caching library for Android focused on smooth scrolling
https://bumptech.github.io/glide/
Other
34.67k stars 6.12k forks source link

Glide won't cache Local Image in Disk Cache #2438

Closed andrewmunn closed 6 years ago

andrewmunn commented 7 years ago

Glide Version: 4.1.1

Integration libraries: Okhttp3

Device/Android Version: Pixel XL 8.0.0

Issue details / Repro steps / Use case background: I want to cache local photos the user takes with the in-app camera at original resolution in the Glide disk cache, because after I upload the images to the server, I delete the local copy. By preloading the image into Glide, it ensures it will be available without re-fetching from the CDN after upload.

However,

requestManager.download(localImageUri);

doesn't seem to do the trick. After I delete the local image, Glide can no longer display that Uri and reports:

W/Glide: Load failed for file:///data/user/0/co.mypackage.app/files/images/JPEG_20170928_154858_120536732194062722.jpg with size [1440x2560]
         class com.bumptech.glide.load.engine.GlideException: Failed to load resource
           Cause (1 of 2): class com.bumptech.glide.load.engine.GlideException: Fetching data failed, class java.io.InputStream, LOCAL
             Cause (1 of 1): class java.io.FileNotFoundException: /data/user/0/co.mypackage.app/files/images/JPEG_20170928_154858_120536732194062722.jpg (No such file or directory)
           Cause (2 of 2): class com.bumptech.glide.load.engine.GlideException: Fetching data failed, class android.os.ParcelFileDescriptor, LOCAL
             Cause (1 of 1): class java.io.FileNotFoundException: open failed: ENOENT (No such file or directory)

This appears to be a bug. The download method uses DiskCacheStrategy.DATA, which should copy all request data into the disk cache, regardless if the DataSource is LOCAL or not.

Am I doing it wrong? bug? Or intended behavior?

Thanks, and thanks for an awesome library!

sjudd commented 7 years ago

What request are you using to load the image after you expect it to be cached? In particular, what DiskCacheStrategy are you using?

andrewmunn commented 7 years ago

After I cache the image, I'm using the default disk cache strategy. Usually a very simple request like requestManager.load(localUri).into(myImageView)

sjudd commented 7 years ago

Thanks, I'm not able to reproduce this if I make sure the cache keys are the same. Two quick points:

  1. You need to call .submit() or .into() when you use .download(). There's now an IDE error that will show if you don't use the result of .download(), but it's an easy mistake to make.
  2. The signature affects the data cache key, so if you're using a signature in either request, you need to make sure it's the same in both places.

I got bit by 2. above when looking in to this. I'd suspect your issue is the first one though from the line you pasted in your original question (sorry I didn't notice it earlier).

stale[bot] commented 7 years ago

This issue has been automatically marked as stale because it has not had activity in the last seven days. It will be closed if no further activity occurs within the next seven days. Thank you for your contributions.