Images not loading in Fresco
Asked Answered
G

6

24

I'm trying to load images in Fresco, but the images don't seem to load. When I add a placeholder view the placeholder shows.

public static void loadImg(final Context c, final SimpleDraweeView view, final String img){
    Uri uri = Uri.parse("https://placekitten.com/g/201/300");
    view.setImageURI(uri);
}

Note, I am initializing Fresco in the Application, and I do have internet usage in the Manifest.

SimpleDraweeView

        <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/small_box_image"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        fresco:placeholderImage="@drawable/google"
        fresco:fadeDuration="300"
        fresco:viewAspectRatio="1.33"
        />

Method calling loadImg

public static View generateTileView(Context c, Tile t){

    LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view;
    boolean desc = false;

    switch(t.getObjectTypeName()){
        case Tile.OBJECT_NAME:
            view = inflater.inflate(R.layout.view_tile_small_object, null);
            break;
        case Tile.LOOK_CLOSER_NAME:
            view = inflater.inflate(R.layout.view_tile_small_look_closer, null);
            desc = true;
            break;
        case Tile.STORY_NAME:
            view = inflater.inflate(R.layout.view_tile_small_story, null);
            desc = true;
            break;
        case Tile.TOUR_NAME:
            view = inflater.inflate(R.layout.view_tile_small_tour, null);
            desc = true;
            break;
        default:
            view = inflater.inflate(R.layout.view_tile_small_object, null);
            break;
    }

    TextView type = (TextView) view.findViewById(R.id.small_box_type);
    TextView title = (TextView) view.findViewById(R.id.small_box_title);
    SimpleDraweeView img = (SimpleDraweeView) view.findViewById(R.id.small_box_image);

    if(desc) {
        TextView descText = (TextView) view.findViewById(R.id.small_box_desc);
        if ( t.getDescription() != null) {
            descText.setText(t.getDescription());
            descText.setTypeface(TypefaceUtil.get(c, TypefaceUtil.ENZO_M));
        }else{
            descText.setVisibility(View.GONE);
        }
    }

    title.setText(t.getTitle());
    title.setTypeface(TypefaceUtil.get(c, TypefaceUtil.SOFIA_BOLD));
    type.setText(t.getObjectTypeName());
    type.setTypeface(TypefaceUtil.get(c, TypefaceUtil.SOFIA_BOLD));
    ViewUtils.loadImg(c, img, t.getImg());

    return view;
}



public static void injectTileViews(Context c, final OnFragmentChangeListener listener,
                        List<Tile> tiles, LinearLayout leftSide, LinearLayout rightSide){

    int index = 0;

    for(final Tile t : tiles){
        //Gets the views and the ImageView for the star
        View v = ViewUtils.generateTileView(c,t);
        if(!t.getObjectTypeName().equals(Tile.LOOK_CLOSER_NAME)) {
            final ImageView star = (ImageView) v.findViewById(R.id.tile_star);

            //Sets the favorite on click listener
            star.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String type = "";
                    switch(t.getObjectTypeName()){
                        case Tile.OBJECT_NAME:
                            type = OnFragmentChangeListener.OBJECT;
                            break;
                        case Tile.STORY_NAME:
                            type = OnFragmentChangeListener.STORY;
                            break;
                        case Tile.TOUR_NAME:
                            type = OnFragmentChangeListener.TOUR;
                            break;
                    }
                    t.setFavorite(!t.getFavorite());
                    listener.onFavorite(type, t.getObjectID(), t.getFavorite());
                    star.setImageResource((t.getFavorite()) ? R.drawable.ic_grade : R.drawable.favorite_star);
                }
            });

            //sets the stat image based on whether it's favorite or no
            star.setImageResource((t.getFavorite()) ? R.drawable.ic_grade : R.drawable.favorite_star);
        }
        //sets the entire tile click listener
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (t.getObjectTypeName()) {
                    case Tile.OBJECT_NAME:
                        listener.onFragmentChange(MainActivity.OBJECT, t.getObjectID());
                        break;
                    case Tile.TOUR_NAME:
                        listener.onFragmentChange(MainActivity.TOUR, t.getTourID());
                        break;
                    case Tile.STORY_NAME:
                        listener.onFragmentChange(MainActivity.STORY, t.getStoryID());
                        break;
                    case Tile.LOOK_CLOSER_NAME:
                        listener.onFragmentChange(MainActivity.LOOK_CLOSER, t.getObjectID());
                        break;
                }
            }
        });

        //invalidates the measurement
        leftSide.invalidate();
        rightSide.invalidate();
        //sets the measure spec
        final int widthMeasureSpec =
                View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.MATCH_PARENT,View.MeasureSpec.UNSPECIFIED);
        final int heightMeasureSpec =
                View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.UNSPECIFIED);
        //re-measures
        leftSide.measure(widthMeasureSpec, heightMeasureSpec);
        rightSide.measure(widthMeasureSpec, heightMeasureSpec);
        int l = leftSide.getMeasuredHeight();
        int r = rightSide.getMeasuredHeight();

        //determines which side to add the view to
        if(l <= r){
            leftSide.addView(v);
        }else{
            rightSide.addView(v);
        }

        //sets the bottom margin since it needs to be part of the parent before we
        //can get the layout params
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) v.getLayoutParams();
        params.setMargins(0, 0, 0, (int) c.getResources().getDimension(R.dimen.margin_normal));
        v.setLayoutParams(params);
        v.requestLayout();

        //adds the index
        index++;
    }
}

Initializing

public class MyApplication extends Application {

public static DisplayMetrics metrics;
private Typeface exoBlack;
private Tracker mTracker;

@Override
public void onCreate() {
    super.onCreate();

    LeakCanary.install(this);
    WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
    metrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(metrics);
    Fresco.initialize(this);
}
}

Sample Log

12-22 15:52:47.424 32462 32462 V unknown:AbstractDraweeController: controller 77d4657 null -> 0: initialize
12-22 15:52:47.425 32462 32462 V unknown:AbstractDraweeController: controller 77d4657 0: setHierarchy: com.facebook.drawee.generic.GenericDraweeHierarchy@a0fa544
12-22 15:52:47.435 32462 32462 V unknown:AbstractDraweeController: controller 3a5f0f3 null -> 1: initialize
12-22 15:52:47.435 32462 32462 V unknown:AbstractDraweeController: controller 3a5f0f3 1: setHierarchy: com.facebook.drawee.generic.GenericDraweeHierarchy@c03d1b0
12-22 15:52:47.442 32462 32462 V unknown:AbstractDraweeController: controller ef0f7ae null -> 2: initialize
12-22 15:52:47.442 32462 32462 V unknown:AbstractDraweeController: controller ef0f7ae 2: setHierarchy: com.facebook.drawee.generic.GenericDraweeHierarchy@6a4a94f
12-22 15:52:47.448 32462 32462 V unknown:AbstractDraweeController: controller 1cc58dc null -> 3: initialize
12-22 15:52:47.449 32462 32462 V unknown:AbstractDraweeController: controller 1cc58dc 3: setHierarchy: com.facebook.drawee.generic.GenericDraweeHierarchy@d3d0de5
12-22 15:52:47.455 32462 32462 V unknown:AbstractDraweeController: controller 158e0ba null -> 4: initialize
12-22 15:52:47.455 32462 32462 V unknown:AbstractDraweeController: controller 158e0ba 4: setHierarchy: com.facebook.drawee.generic.GenericDraweeHierarchy@12c8b6b
12-22 15:52:47.459 32462 32462 V unknown:AbstractDraweeController: controller 3a5f0f3 1: onAttach: request needs submit
12-22 15:52:47.463 32462 32462 V unknown:AbstractDraweeController: controller 3a5f0f3 1: submitRequest: dataSource: ab5a361
12-22 15:52:47.464 32462 32462 V unknown:AbstractDraweeController: controller 1cc58dc 3: onAttach: request needs submit
12-22 15:52:47.465 32462 32462 V unknown:AbstractDraweeController: controller 1cc58dc 3: submitRequest: dataSource: 5244e86
12-22 15:52:47.465 32462 32462 V unknown:AbstractDraweeController: controller 77d4657 0: onAttach: request needs submit
12-22 15:52:47.466 32462 32462 V unknown:AbstractDraweeController: controller 77d4657 0: submitRequest: dataSource: 2067347
12-22 15:52:47.466 32462 32462 V unknown:AbstractDraweeController: controller ef0f7ae 2: onAttach: request needs submit
12-22 15:52:47.467 32462 32462 V unknown:AbstractDraweeController: controller ef0f7ae 2: submitRequest: dataSource: 3c8e774
12-22 15:52:47.467 32462 32462 V unknown:AbstractDraweeController: controller 158e0ba 4: onAttach: request needs submit
12-22 15:52:47.468 32462 32462 V unknown:AbstractDraweeController: controller 158e0ba 4: submitRequest: dataSource: ac8149d
12-22 15:52:47.481 32462 32528 V unknown:BufferedDiskCache: Did not find image for https://placekitten.com/g/201/300 in staging area
12-22 15:52:47.481 32462 32528 V unknown:BufferedDiskCache: Disk cache read for https://placekitten.com/g/201/300
12-22 15:52:47.483 32462 32528 V unknown:BufferedDiskCache: Disk cache miss for https://placekitten.com/g/201/300
12-22 15:52:58.837   491   491 V unknown:AbstractDraweeController: controller 77d4657 null -> 0: initialize
12-22 15:52:58.837   491   491 V unknown:AbstractDraweeController: controller 77d4657 0: setHierarchy: com.facebook.drawee.generic.GenericDraweeHierarchy@a0fa544
12-22 15:52:58.847   491   491 V unknown:AbstractDraweeController: controller 3a5f0f3 null -> 1: initialize
12-22 15:52:58.848   491   491 V unknown:AbstractDraweeController: controller 3a5f0f3 1: setHierarchy: com.facebook.drawee.generic.GenericDraweeHierarchy@c03d1b0
12-22 15:52:58.856   491   491 V unknown:AbstractDraweeController: controller ef0f7ae null -> 2: initialize
12-22 15:52:58.856   491   491 V unknown:AbstractDraweeController: controller ef0f7ae 2: setHierarchy: com.facebook.drawee.generic.GenericDraweeHierarchy@6a4a94f
12-22 15:52:58.862   491   491 V unknown:AbstractDraweeController: controller 1cc58dc null -> 3: initialize
12-22 15:52:58.863   491   491 V unknown:AbstractDraweeController: controller 1cc58dc 3: setHierarchy: com.facebook.drawee.generic.GenericDraweeHierarchy@d3d0de5
12-22 15:52:58.869   491   491 V unknown:AbstractDraweeController: controller 158e0ba null -> 4: initialize
12-22 15:52:58.869   491   491 V unknown:AbstractDraweeController: controller 158e0ba 4: setHierarchy: com.facebook.drawee.generic.GenericDraweeHierarchy@12c8b6b
12-22 15:52:58.872   491   491 V unknown:AbstractDraweeController: controller 3a5f0f3 1: onAttach: request needs submit
12-22 15:52:58.878   491   491 V unknown:AbstractDraweeController: controller 3a5f0f3 1: submitRequest: dataSource: ab5a361
12-22 15:52:58.878   491   491 V unknown:AbstractDraweeController: controller 1cc58dc 3: onAttach: request needs submit
12-22 15:52:58.879   491   491 V unknown:AbstractDraweeController: controller 1cc58dc 3: submitRequest: dataSource: 5244e86
12-22 15:52:58.879   491   491 V unknown:AbstractDraweeController: controller 77d4657 0: onAttach: request needs submit
12-22 15:52:58.880   491   491 V unknown:AbstractDraweeController: controller 77d4657 0: submitRequest: dataSource: 2067347
12-22 15:52:58.880   491   491 V unknown:AbstractDraweeController: controller ef0f7ae 2: onAttach: request needs submit
12-22 15:52:58.881   491   491 V unknown:AbstractDraweeController: controller ef0f7ae 2: submitRequest: dataSource: 3c8e774
12-22 15:52:58.881   491   491 V unknown:AbstractDraweeController: controller 158e0ba 4: onAttach: request needs submit
12-22 15:52:58.882   491   491 V unknown:AbstractDraweeController: controller 158e0ba 4: submitRequest: dataSource: ac8149d
12-22 15:52:58.898   491   535 V unknown:BufferedDiskCache: Did not find image for https://placekitten.com/g/201/300 in staging area
12-22 15:52:58.899   491   535 V unknown:BufferedDiskCache: Disk cache read for https://placekitten.com/g/201/300
12-22 15:52:58.904   491   535 V unknown:BufferedDiskCache: Disk cache miss for https://placekitten.com/g/201/300

Manifest

    <application
    android:name=".utils.MyApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:logo="@drawable/home"
    android:screenOrientation="portrait"
    android:theme="@style/AppTheme" >
Graphics answered 22/12, 2015 at 18:35 Comment(30)
does your SimpleDraweeView have a fixed size?Abbess
I have a fixed height, and a match parent widthGraphics
that's not enough. To do so you should call setAspectRatio on the SimpleDraweeView's instance. Try setting a fixed size for both width and heightAbbess
updated my XML to add fresco:viewAspectRatio and still no image loadingGraphics
android:layout_width="wrap_content" and try againAbbess
Still no image loadingGraphics
update your question. Post more core code.Abbess
are you adding view to some sort of container? Can you at least see the placeholders ?Abbess
added code that adds the returned view to a parent container.Graphics
Could you post how you are initializing Fresco? Does it work if you try with a different images source?Abbess
I've tried assets from a few different places. I added the initialization. Thanks for the help.Graphics
Use this instead of getApplicationContext () . is this the Application's subclass registered in the manifest?Abbess
no luck. I thing I originally had it like that, but changed it just to try.Graphics
is the Application's subclass registered in the manifest?Abbess
As in the MyApplication ?Graphics
Yes, do you have it in your Manifest?Abbess
No I did not. I know how to add Services, and Activities, but how do I add just the Application?Graphics
In the tag <application add android:name with the fully qualified path to your application. As you normally do for your activitiesAbbess
Yeah it is in there, sorry about that.Graphics
I quickly run a test, and it is working correctly for me. The problem lies somewhere elseAbbess
Can you post logs following the instructions in frescolib.org/docs/… ?Abigail
The Sample Log file in my post is a log output from when I ran the following code.Graphics
@Abbess Any idea what else it could be? I pretty much just swapped out Picasso for Fresco because of some of Frescos functionality. It works no problem when I'm on a branch running Picasso.Graphics
is the placeholder shown ? What happens if you force fixed width and height and get rid of the aspect ratio ?Abbess
do you have the code on github ?Abbess
Placeholders do show, I'll try fixed width and height right now. And no, this is an issue on a work project so the code is not on a public repo.Graphics
Fixed height and width no nothing besides show the placeholder imageGraphics
Could you post your full AndroidManifest.xml ?Healall
@PeterChappy were u able to find a solution for this?Translative
Did you add INTERNET permission in the manifest?Hypostatize
A
8

Try this

SimpleDraweeView imageView= (SimpleDraweeView) v.findViewById(R.id.full_ad_unit);

Uri imageUri = Uri.parse(imagePath);

ImageRequest request = ImageRequest.fromUri(imageUri);

DraweeController controller = Fresco.newDraweeControllerBuilder()
    .setImageRequest(request)
    .setOldController(imageView.getController()).build();

Log.e(TAG, "ImagePath uri " + imageUri);

imageView.setController(controller);
Aesculapian answered 14/1, 2016 at 9:42 Comment(0)
C
2

I don't know the logic but Fresco prefers to get the Uri from a file. First convert your uri to a file and then get Uri from that file. It works for me on local storage.

public static void loadImg(final Context c, final SimpleDraweeView view, final String img){
    Uri uri = Uri.parse("https://placekitten.com/g/201/300");
    view.setImageURI(Uri.fromFile(new File(uri)));
}
Casi answered 8/1, 2016 at 4:41 Comment(9)
thnaks. I'll give this a try in the morning.Graphics
I found Glide much faster and easier solution at least for locally stored files. Give it a try as there is many positive feedbacks about it's use for getting images from net too. Just a suggestion github.com/bumptech/glideCasi
My #1 reason for using Fresco is it has built in simultaneous loading of a super low res image and a high res image. I tried to implement it with Picasso and had no luck. Am I able to do that with glide?Graphics
I'm doing something like that but to get low and high resolution images from local storage by forcing Glide to get an specific size like 50*50 px, by ".overide(50, 50)". Tried this before?Casi
Am I able to not get them from local storage?Graphics
I wasn't in need for getting images or videos from the net, but I think if there was a problem they would announce it. But about getting a specific size i'm pretty sure you could do it with "override(int, int) method. You could get Thumbnail first and then Glide automatically get's the full image.Casi
"Thumbnail support - You can now load multiple images into the same view at the same time so you can minimize the amount of time your users spend looking at loading spinners without sacrificing quality. To first load a thumbnail at 1/10th the size of your view and then load the full image on top, use: Glide.with(yourFragment).load(yourUrl).thumbnail(0.1f).into(yourView). For more control you can also pass in a completely new request into the .thumbnail() call." github.com/bumptech/glide/wikiCasi
Awesome. Thanks I'll check it out.Graphics
I both used Glide and Fresco to get images from Gallery so local storage. Definitely Fresco is better option in terms of loading images faster at first time.Genovese
P
2

If you have problem with LOADING BIG IMAGES, just enable Downsampling when configuring the image pipeline.

Write your Fresco initialisation like this

ImagePipelineConfig config = ImagePipelineConfig.newBuilder(this)
            .setDownsampleEnabled(true)
             ...
            .build();
Fresco.initialize(this, config);
Podvin answered 6/1, 2018 at 21:59 Comment(0)
C
2

I know this is an old question, but for those who enter here: android:layout_width="wrap_content" and android:layout_height="wrap_content" are NOT supported by Fresco. This question has already been fully answered here.

Company answered 24/3, 2018 at 21:10 Comment(0)
L
0

Similar issue is reported at the fresco repository on GitHub HERE (in the comments below the main reported issue)

As per the comments on the issue by the developer who reported it:

It seems to be a random thing : it's ok for most images. When cache is cleared for testing, the few images that are "not loaded" aren't the same between runs. I've "200" response from apache for image that are correctly loaded and display (apache log). And no log at all for random crashing images.

I would suggest trying the following suggestions by other developers for the issue:

  1. (From the issue report page) Clear the cache and try again.
  2. (From the issue report page) Use an HTTP proxy/monitor to find out what response you get from the server. If you get a 204 No Content the server might not accept the user-agent header.
  3. (My suggestion) Try loading an image using an altogether different url and see if it happens for every image or just this one. If it is just this one problem could be with the image.

Another report of the same issue HERE - But this one does not have any concrete replies with solutions yet.

You may also want to subscribe to notifications on these post in case someone posts a really good solution soon and it gets merged. You could then just get the latest version and chill. :) (In case any of the above do not work out for you.)

Lemley answered 8/1, 2016 at 19:10 Comment(0)
M
0

you need a direct link, for example: https://i.sstatic.net/sEObL.jpg

and you should add file type end of uri. for this case: ".jpg"

Uri uri = Uri.parse("https://i.sstatic.net/sEObL.jpg");

Mnemosyne answered 13/1, 2016 at 14:19 Comment(1)
My problem occurs in Android 4.4 when the url contains https. But if the url is http the image is displayed. Above Android 5.0 the images are uploaded wthout a problem.Archlute

© 2022 - 2024 — McMap. All rights reserved.