【问题标题】:Glide images sometimes are not loading滑翔图像有时无法加载
【发布时间】:2016-09-05 17:38:19
【问题描述】:

是一个问答游戏,答案可以是文本或图像,我正在使用 Gilde 图像加载它,但对于某些用户来说,即使互联网良好且图像质量低,图像也无法加载。

我将复制所有代码,因为我不知道是否是 Gilde 问题。

if (AppConstant.arQuestionList.get(questionid).getArAnswer().get(i).getAtype()
                    .equalsIgnoreCase("image")) {
                txtOption = new ImageView(this);

                try {
                    Glide.with(QuestionActivity.this).load(AppConstant.arQuestionList.get(questionid)
                            .getArAnswer().get(i).getAnswer()).override(60, 60).into((ImageView)
                            txtOption);
                }catch (OutOfMemoryError e){
                    e.printStackTrace();
                }
                txtOption.setPadding(5, 5, 5, 5);

            } else {
                txtOption = new TextView(this);

                ((TextView) txtOption).setText(AppConstant.arQuestionList.get(questionid)
                        .getArAnswer().get
                                (i).getAnswer());
                ((TextView) txtOption).setTextColor(getResources().getColor(R.color.answer_color));
                ((TextView) txtOption).setTypeface(tf, Typeface.BOLD);
                ((TextView) txtOption).setGravity(Gravity.CENTER);
                txtOption.setPadding(10, 20, 10, 20);
            }

布局

    <LinearLayout
        android:id="@+id/answerwrap"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/dp_30"
        android:layout_marginRight="@dimen/dp_30"
        android:layout_marginTop="@dimen/dp_30"
        android:orientation="vertical">

    </LinearLayout>

【问题讨论】:

标签: android android-glide


【解决方案1】:

我在这里解释的使用 glide 的最佳方式。

//crete this method into your Utils class and call this method wherever you want to use.
//you can set these placeHolder() and error() image static as well. I made it as comment inside this method, then no need to use [placeHolderUrl and errorImageUrl] parameters. remove it from this method.
public static void loadImage(final Activity context, ImageView imageView, String url, int placeHolderUrl, int errorImageUrl) {
    if (context == null || context.isDestroyed()) return;

    //placeHolderUrl=R.drawable.ic_user;
    //errorImageUrl=R.drawable.ic_error;
        Glide.with(context) //passing context 
                .load(getFullUrl(url)) //passing your url to load image.
                .placeholder(placeHolderUrl) //this would be your default image (like default profile or logo etc). it would be loaded at initial time and it will replace with your loaded image once glide successfully load image using url.
                .error(errorImageUrl)//in case of any glide exception or not able to download then this image will be appear . if you won't mention this error() then nothing to worry placeHolder image would be remain as it is.
                .diskCacheStrategy(DiskCacheStrategy.ALL) //using to load into cache then second time it will load fast.
                .animate(R.anim.fade_in) // when image (url) will be loaded by glide then this face in animation help to replace url image in the place of placeHolder (default) image.
                .fitCenter()//this method help to fit image into center of your ImageView 
                .into(imageView); //pass imageView reference to appear the image.
}

fade_in.xml(放入 res->anim)

<set xmlns:android="http://schemas.android.com/apk/res/android">
<!--THIS ANIMATION IS USING FOR FADE IN -->

<alpha
    android:duration="800"
    android:fromAlpha="0.0"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:toAlpha="1.0" />

最后调用这个方法

来自您的活动。

Utils.loadImage(YourClassName.this,mImageView,url,R.drawable.ic_user,R.drawable.ic_error);

来自您的片段

Utils.loadImage(getActivity,mImageView,url,R.drawable.ic_user,R.drawable.ic_error);

【讨论】:

    【解决方案2】:

    我已经关注了一切。

    添加.dontAnimate 添加了所有必要的权限、Internet、Access_Network_State

    但没有任何效果。

    终于找到了图片无法加载的原因,我的图片网址没有httphttps。我在我的图片网址前面添加了http://,它可以完美加载。

    【讨论】:

      【解决方案3】:

      在我的 Glide 4.8.0 中,将以下内容添加到 Glide 正在加载的 ImageView 小部件中起到了作用,虽然很奇怪,但它可以工作....

      android:background="?attr/selectableItemBackgroundBorderless"
      

      也可以

      android:background="@drawable/R.drawable.yourResourceFile
      

      也可以

      android:background="@android:color/transparent"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-05
        • 1970-01-01
        • 2017-07-12
        • 2020-05-10
        • 1970-01-01
        • 2020-01-15
        • 1970-01-01
        相关资源
        最近更新 更多