-
Notifications
You must be signed in to change notification settings - Fork 6.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Loading images from assets #155
Comments
Thanks for reporting this, in theory the Uri is sufficient. What failure do you see (if you don't see any exception you can turn on logging using adb shell setprop log.tag.GenericRequest, see https://github.com/bumptech/glide/wiki/Debugging-and-Error-Handling)? If using the AssetManager is a requirement you can always define a custom ModelLoader that uses the AssetManager to return an InputStream and pass it in to your request with the
If we need to use the AssetManager to parse these urls rather than just relying on a ContentResolver, the best fix for the library would be to modify the existing UriLoader and adding another case if we're given an asset Uri. |
I get this exception thrown:
However the file does exist in the asset folder. By using a custom loader/fetcher, I manage to load those files properly. Here are the corresponding classes: https://gist.github.com/vpratfr/5e61fd452a27e964ea63 Then I am loading the assets with:
So something is wrong with the default loading, I don't know if that's a bug on your side or on mine. One problem remains : inside a GridView (probably in a ListView too), I cannot see the image without scrolling the view out of sight and getting it back on screen. Does it have something to do with caching? Or with my custom loader/fetcher? |
Thanks for the code and report I'm looking into asset manager uris. I'm not sure where to point you to with regards to the GridView issue. Glide's Flickr sample app uses grid views without a problem. The only problem I see in your code is that you don't need to use a weak reference for a Context in your DataFetcher. Instead just pass in the application Context (context.getApplicationContext()). That said, I don't think using a weak reference would cause the image to fail to load. Do you see a debug log when the image fails to load the first time? What width and height do your views have? |
As it should be loaded
As @vpratfr wants to load it
it gives the following messages:
and it is because Why Picasso workswe want the following to work (which is roughly equivalent to the above
It fails because Glide doesn't handle the SolutionAdd Other commentsAs for the I agree that these kind of Uris should be supported for ease of use, but why don't you put your images into |
Hi, Currently doing some other tasks, will try to debug the GridView in the afternoon. Thanks for having had a look at it. I think too the right thing to do would be to add an additional if statement in the UriLoader class to detect assets and use an appropriate Fetcher. I have not directly implemented it in the library and submitted a PR because I still have not taken the time to understand the Model things and would not want to break something (and honestly don't have much because my client is pushing). From the code I sent I think that should be an easy fix though.
Because the file name is dynamically determined. Those are flags and I get the country list from a webservice. The flag name is taken from the ISO code (for instance FR.png). If I was using a resource, that name would get compiled to an int (R.raw.FR) and I would need some reflection magic (or an ugly switch statement) to link the ISO code to the flag resource. Hence the asset is required in my case. |
... or you could use the method provided by the framework: public static int getRawResourceID(Context context, String rawResourceName) {
return context.getResources().getIdentifier(rawResourceName, "raw", context.getPackageName());
}
public static int getDrawableResourceID(Context context, String drawableResourceName) {
return context.getResources().getIdentifier(drawableResourceName, "drawable", context.getPackageName());
} I'm doing the same, I have a DB table for categories and the icon name is stored there (I'm loading SVG files from Or if you really want to pass Uri around:
This even works if you put it in XML ( |
Ok. Back with some tests. I have implemented SVG support (using your sample as a base) and I am now using the method you describe (SVGs in the raw resource folder and some low-res versions in the drawable resource folder). More info about the GridView bug: SVGs get properly loaded in that view. Drawables however are only loaded when the GridView row is out of sight and back (using an adapter and following the ViewHolder pattern). So my conclusion : Picasso is doing something different than Glide when it comes to setting a drawable. My first guess : it may be related to an issue logged in the RoundedImageView repo if you are using the setImageURI method and Picasso is using another method (like directly calling setImageResource). This would explain too why SVGs get shown properly as they are not using the setImageURI method but probably setImageDrawable instead. |
What api level are you using? Not 4.0.4 by any chance? If you use Glide uses setImageDrawable, not setImageUri. Using Thanks for looking more into this! |
ANDROID_BUILD_MIN_SDK_VERSION=15 Testing on Android 4.4.4 (Galaxy Note 2 with Slimkat ROM) |
It works when using asBistmap |
Any chance you could add a bit of code or a sample project demonstrating the issue? Even just the xml you're using to specify your grid and the items in it would be helpful. As I mentioned earlier I know the drawables can work in a GridView because the Flickr sample works fine, but clearly something is a bit different here. Thanks for the follow up. |
I spent a little while looking in to this, it seems like RoundedImageView doesn't handle TransitionDrawables quite right. Removing the crossfade using I think the ideal solution is to follow the pattern the author setup for Picasso and create your own custom Glide transformation: https://github.com/vinc3m1/RoundedImageView/blob/master/roundedimageview/src/main/java/com/makeramen/RoundedTransformationBuilder.java. I'll leave this open to track the asset manager uris, but otherwise everything seems WAI from Glide's point of view. |
Hi, Thanks in advance. |
@androiddeveloperPooja30 Check out the wiki on cache invalidation: https://github.com/bumptech/glide/wiki/Caching-and-Cache-Invalidation#custom-cache-invalidation. You will want to pass in a new signature each time you update the file. |
Hi,
I have switched my current project from Picasso to Glide. However, I cannot manage to load an image from the application assets. Is that even possible?
Sample code:
I have also tried the load(String) method without success and I don't see a load method taking for instance an InputStream or a FileDescriptor I could have obtained with the AssetManager class.
That exact same code was working in Picasso.
Glide is working properly in the rest of the app and loading nicely remote images.
The text was updated successfully, but these errors were encountered: