java在Android 7上使用Glide通过URL在fragment ImageView中显示GIF图像+
我试图通过JSON文件中的URL显示GIF图像。为此,我有一个函数,它接受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
安卓:id="@+id/progress"
安卓:layout_width="150dp"
安卓:layout_height="150dp"
安卓:layout_centerVertical="true"
安卓:layout_centerHorizontal="true"
安卓:visibility="visible" />
<ImageView
安卓:id="@+id/imageView"
安卓:layout_width="240dp"
安卓:layout_height="240dp"
安卓:layout_centerVertical="true"
安卓:layout_centerHorizontal="true"
安卓: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:安卓="http://schemas.安卓.com/apk/res/安卓"
package="com.example.gifapp">
<uses-feature 安卓:name="安卓.hardware.wifi" 安卓:required="true" />
<uses-permission 安卓:name="安卓.permission.INTERNET"/>
<uses-permission 安卓:name="安卓.permission.ACCESS_NETWORK_STATE"/>
<application
安卓:allowBackup="true"
安卓:fullBackupContent="@xml/my_backup_rules"
安卓:icon="@mipmap/ic_launcher"
安卓:label="@string/app_name"
安卓:roundIcon="@mipmap/ic_launcher_round"
安卓:supportsRtl="true"
安卓:theme="@style/AppTheme">
<activity 安卓:name=".MainActivity">
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
# 1 楼答案
在调试期间,我发现
.listener
调用了onLoadFailed(...)
方法。所以,在那之后,我跌跌撞撞地找到了一个suitable problem。在安卓7+上,这是因为新的安全配置