图片未在Fresco中加载

时间:2015-12-22 18:35:15

标签: android fresco

我试图在Fresco中加载图片,但图片似乎无法加载。当我添加占位符视图时,占位符显示。

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);
}

注意,我正在应用程序中初始化Fresco,而且我在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"
        />

调用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++;
    }
}

初始化

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);
}
}

示例日志

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

清单

    <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" >

6 个答案:

答案 0 :(得分:7)

试试这个

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);

答案 1 :(得分:2)

如果加载大图像有问题,请在配置图像管道时启用下采样。<​​/ p>

像这样编写你的Fresco初始化

ImagePipelineConfig config = ImagePipelineConfig.newBuilder(this)
            .setDownsampleEnabled(true)
             ...
            .build();
Fresco.initialize(this, config);

答案 2 :(得分:2)

我知道这是一个老问题,但对于那些进入这里的人: Fresco不支持android:layout_width="wrap_content"android:layout_height="wrap_content"。这个问题已经得到了充分的回答here

答案 3 :(得分:0)

我不知道逻辑,但Fresco更喜欢从文件中获取Uri。首先将您的uri转换为文件,然后从该文件中获取Uri。它适用于本地存储。

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)));
}

答案 4 :(得分:0)

在GitHub HERE的fresco存储库中报告了类似的问题(在主要报道的问题下面的评论中)

根据报告的开发人员对该问题的评论:

  

这似乎是一个随机的事情:大多数图像都可以。当缓存是   清除了测试,少数图像是&#34;未加载&#34;不是   运行之间相同。我&#34; 200&#34; apache对图像的响应   正确加载并显示(apache日志)。而且根本没有记录   随机崩溃的图像。

我建议其他开发人员针对此问题尝试以下建议:

  1. (From the issue report page)清除缓存并重试。
  2. (From the issue report page)使用HTTP代理/监视器查找从服务器获得的响应。如果您收到204 No Content,则服务器可能不接受用户代理标头。
  3. (My suggestion)尝试使用完全不同的网址加载图片,看看它是针对每张图片还是仅针对此图片。如果只是这个问题可能与图像有关。
  4. 同一问题的另一份报告HERE - 但是这个问题对解决方案没有任何具体的答复。

    您可能还想订阅这些帖子上的通知,以防有人很快发布一个非常好的解决方案并将其合并。然后你可以获得最新版本并冷静下来。 :)(如果以上任何一项都不适合您。)

答案 5 :(得分:0)

您需要直接链接,例如:http://i.imgur.com/IJF8x9D.jpg

你应该添加uri的文件类型结尾。对于这种情况:“.jpg”

Uri uri = Uri.parse(“http://i.imgur.com/IJF8x9D.jpg”);