Coil: Coil fails to load local image.

Created on 16 Jan 2020  路  3Comments  路  Source: coil-kt/coil

First, let me just say that this library is flippin' awesome & the "question" I'm asking is prolly BIAD (b/c I'm a dummy).

Problem
Coil fails to load local image into an ImageView.

In the following code, I have a picture in the /sdcard/Android/data. I load it in via imageView.load().
val filePath = "/storage/emulated/0/Android/data/PACKAGE_ID/files/Pictures/PIC_2020-01-16_15:39:35:648.jpg"
imageView.load(filePath)

The problem is the bitmap never loads into my ImageView & no errors are displayed, but everything works when doing...
val bmp = BitmapFactory.decodeFile(filePath)
imageView.setImageBitmap(bmp)

Expected behavior
The imageView should load the bitmap.

Version
coil v0.9.1
Pixel XL running Android Q.

Questions
Any idea why coil fails to load the image?
Is there a way I can tell if coil fails to load the bitmap in the code (like a success/error listener)?

question

Most helpful comment

Thanks! To fix your issue you need to append file:// to the beginning:

// Note the 3 forward slashes.
imageView.load("file:///storage/emulated/0/Android/data/PACKAGE_ID/files/Pictures/PIC_2020-01-16_15:39:35:648.jpg")

// Optionally you could pass it as a File instead.
imageView.load(File("/storage/emulated/0/Android/data/PACKAGE_ID/files/Pictures/PIC_2020-01-16_15:39:35:648.jpg"))

Unlike Glide, Coil doesn't autodetect file paths. This is mostly a design choice as load(String) is meant for uris, which should always have a prefix.

Also you can listen to the result of a request by looking at the logs (and enabling them for debug builds: CoilLogger.setEnabled(BuildConfig.DEBUG) or by setting a listener:

imageView.load(path) {
    listener(
        onError = {}
    )
}

All 3 comments

Thanks! To fix your issue you need to append file:// to the beginning:

// Note the 3 forward slashes.
imageView.load("file:///storage/emulated/0/Android/data/PACKAGE_ID/files/Pictures/PIC_2020-01-16_15:39:35:648.jpg")

// Optionally you could pass it as a File instead.
imageView.load(File("/storage/emulated/0/Android/data/PACKAGE_ID/files/Pictures/PIC_2020-01-16_15:39:35:648.jpg"))

Unlike Glide, Coil doesn't autodetect file paths. This is mostly a design choice as load(String) is meant for uris, which should always have a prefix.

Also you can listen to the result of a request by looking at the logs (and enabling them for debug builds: CoilLogger.setEnabled(BuildConfig.DEBUG) or by setting a listener:

imageView.load(path) {
    listener(
        onError = {}
    )
}

That worked. Thanks for your help & for the awesome, kotlin-friendly library!

Hit this as well; file prefix works. or file.toUri()

Was this page helpful?
0 / 5 - 0 ratings