![](/img/trans.png)
[英]Android - Picasso Not Loading URL Image into Fragment ImageView
[英]Show GIF image in fragment ImageView by URL using Glide on Android 7+
我正在尝试通过 URL 显示来自 JSON 文件的 GIF 图像。 为此,我有 function,它采用 JSONObject 并通过 Glide 显示图像。
public class FragmentHandler extends Fragment implements View.OnClickListener {
private void showJsonContent(JSONObject jsonObject) throws JSONException {
if (jsonObject == null) return;
String gifLink = jsonObject.getString("gifURL");
String desc = jsonObject.getString("description");
captionTextView.setText(desc);
Glide.with(Objects.requireNonNull(getActivity())
.getApplicationContext())
.asGif()
.load(gifLink)
.listener(new RequestListener<GifDrawable>() {
@Override
public boolean onLoadFailed(GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
progressBar.setVisibility(View.GONE);
return false;
}
@Override
public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
progressBar.setVisibility(View.GONE);
return false;
}
}).into(new ImageViewTarget<GifDrawable>(imageView) {
@Override
protected void setResource(GifDrawable resource) {
imageView.setImageDrawable(resource);
}
});
}
}
进度条和 XML 中的 ImageView 看起来是这样的:
<ProgressBar
android:id="@+id/progress"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:visibility="visible" />
<ImageView
android:id="@+id/imageView"
android:layout_width="240dp"
android:layout_height="240dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:contentDescription="@string/gif_image" />
它在 Android 6 像素模拟器 (API 26) 上绝对正确,但对于更高版本的设备(物理和模拟器),它不显示图像。 URL 和 JSON 正确,因为程序从另一个 JSON 字段打印消息,我尝试调试它以进行检查。
我在 StackOverflow 上阅读了很多有类似问题的帖子,并尝试了所有建议,主要是关于使用asGif()
。 但这没有帮助。 有什么办法可以解决这个问题吗?
更新:
这是 GIF 图像的 URL 示例:
http://static.devli.ru/public/images/gifs/201402/90400af2-91c1-4f31-bf5e-b2561b598d7c.gif
还有我的 AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gifapp">
<uses-feature android:name="android.hardware.wifi" android:required="true" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:fullBackupContent="@xml/my_backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
在调试过程中我发现, .listener
调用onLoadFailed(...)
方法。 所以,在那之后,我偶然发现了一个合适的问题。 在 Android 7+ 上,它是由于新的安全配置引起的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.