滑动(Glide)无法加载图片。

5

我有一个带有ImageView的简单布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.vbusovikov.glidetest.MainActivity">

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        />

</RelativeLayout>

这里有一个简单的Glide表达式,可用于将图像加载到此ImageView中,以测试Glide:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView imageView = (ImageView)findViewById(R.id.image);

        Glide.with(this)
                .load("http://you-ps.ru/uploads/posts/2013-08/1376601606_1273.png")
                .error(R.mipmap.ic_launcher)
                .into(imageView);

    }

然而,出现了错误图标。 可能是什么问题呢? 我在我的网络上使用代理服务器,并且为这种情况准备了适当的 gradle.properties。

systemProp.http.proxyHost=proxy******.ru
systemProp.http.proxyPort=****

即使我试图在没有代理的情况下启动这个小应用程序,它也因某种原因无法工作。
我的 build.gradle 文件
apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.vbusovikov.glidetest"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.1'

    compile 'com.github.bumptech.glide:glide:3.7.0'

    testCompile 'junit:junit:4.12'
}

更新。这个简单的应用程序可以从互联网加载图片,但无法从我的服务器加载图片。我的一些服务器上的图片能够正常加载,但其他图片加载不出来。我已经迷失了方向。


2
Glide.with(yourclass.this) - IntelliJ Amiya
@PhanVanLinh 我试过了,但是没有成功。 - TrueCH
@OBX 我有这个权限。 - TrueCH
1
@IntelliJAmiya 我也尝试添加了 dontAnimate() 但是没有起作用。 - TrueCH
在这里找到我的答案,https://stackoverflow.com/a/45108669/1068800 - Neela
显示剩余5条评论
10个回答

9
很遗憾,所有的答案都是正确的,但在我的情况下它们并不起作用。服务器设置不适合从中下载图片。
==更新==
过了一会儿,我发现我的服务器上的图片已经损坏了。您可以通过在Mozilla Firefox中打开此URL来检查提供的URL上的图片是否有效。 图片中的最后几千字节可能已被删除,但像Google Chrome这样的浏览器会忽略它并正常显示图像。然而,Firefox更加敏感,因此有助于定位问题。
==更新2==
再过一段时间,我发现不仅损坏的图片会导致问题。在应用程序的清单中添加android:usesCleartextTraffic="true",它将解决一些图片加载问题。

1
我遇到了相同的问题。。在浏览器上图像正常显示,但是Glide库出现了错误。 我尝试了很多解决方案,但问题没有得到解决。 当我使用了你的解决方案后,我的问题得到了解决,谢谢。 - Adel SaadEddin
更新2对我来说可以工作,但会警告API 22(我的最小版本)。还有其他想法吗? - paul_f

2
请在您的图像URL中使用https而不是http。原始答案翻译成“最初的回答”。

就这么简单,这真的为我节省了时间和精力。谢谢 @Hiren - Kenny Dabiri

1

我可能回答晚了,但如果还有人遇到这个问题。 我曾经遇到类似的问题,在使用Glide加载图片时无法加载,经过大量研究后发现问题不是来自Glide而是来自图像URL。 为了解决这个问题,我尝试使用自定义用户代理,并且它对我有效。

GlideUrl url = new GlideUrl(imgUrl, new LazyHeaders.Builder()
                    .addHeader("User-Agent", WebSettings.getDefaultUserAgent(mContext))
                    .build());

并使用此URL加载图像:

Glide.with(mContext).load(url).into(imageview);

1
我将在manifest中添加该属性。
 <application
       ...
        android:usesCleartextTraffic="true"
       ...>
</application>


你的回答可以通过提供更多支持信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的答案是正确的。您可以在帮助中心中找到有关如何编写良好答案的更多信息。 - Community
你的回答有点晚了,伙计 :) 我在2019年的回答中已经提到了明文流量。 - TrueCH

0

这种情况我也遇到过:在模拟器上不显示,但在真实设备上却可以显示。所以我去设置 -> 应用程序,在模拟器上找到你的应用并卸载它,然后再次运行它,它会重新安装,问题就解决了。


0
在我的情况下,我正在使用一个监听器,在onResourceReady中我返回了true,但你应该返回false
   Glide.with(context)
        .load(url)
        .centerCrop()
        .diskCacheStrategy(DiskCacheStrategy.RESOURCE).listener(object :
            RequestListener<Drawable> {
            override fun onLoadFailed(
                e: GlideException?,
                model: Any?,
                target: Target<Drawable>?,
                isFirstResource: Boolean,
            ): Boolean {
                imageView.visibility = GONE
                errorContainer.visibility = VISIBLE
                return true
            }

            override fun onResourceReady(
                resource: Drawable?,
                model: Any?,
                target: Target<Drawable>?,
                dataSource: DataSource?,
                isFirstResource: Boolean,
            ): Boolean {
                imageView.visibility = VISIBLE
                errorContainer.visibility = GONE
                return false  // <<<<<<<<<<<<<<<<<<<<<<<< here
            }

        }).into(imageView)

0
尝试添加占位符标签(看起来很奇怪,但这在我的端上解决了问题)。
Glide.with(YourActivity.this)
                .load("http://you-ps.ru/uploads/posts/2013-08/1376601606_1273.png")
                .error(R.mipmap.ic_launcher)
                .placeholder(R.mipmap.placeholder)
                .into(imageView);

对我不起作用,我也尝试了 dontAnimate(),它也没用。 - TrueCH

0

图片可能太大而无法加载,这可能会触发异常:“位图太大,无法上传到纹理”。在这种情况下,在将图像设置到视图之前应该进行缩放(请参见user1352407's answer

从user1352407的答案中复制:

ImageView iv  = (ImageView)waypointListView.findViewById(R.id.waypoint_picker_photo);
Bitmap d = new BitmapDrawable(ctx.getResources() , w.photo.getAbsolutePath()).getBitmap();
int nh = (int) ( d.getHeight() * (512.0 / d.getWidth()) );
Bitmap scaled = Bitmap.createScaledBitmap(d, 512, nh, true);
iv.setImageBitmap(scaled);

-1

在我的端上它运行得很好...检查网络并不要忘记在清单中添加互联网权限:)


我拥有这个权限。 - TrueCH
你能把你的build.gradle和logcat粘贴在这里吗? :) - Pratik Gondil

-2

清除设备数据,它将完美运行。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接