英文:
Show GIF image in fragment ImageView by URL using Glide on Android 7+
问题
以下是您提供的代码的翻译部分:
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);
}
});
}
}
<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" />
更新:
以下是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>
请注意,这只是您提供的代码的翻译部分,其中涉及的变量名和函数名可能需要根据上下文进行调整。
英文:
I'm trying to show GIF image by URL from JSON file. For that, I have function, which takes JSONObject and show image by 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);
}
});
}
}
Progress Bar and ImageView in XML looks like that:
<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" />
It works absolutely correct on Android 6 Pixel emulator (API 26), but with devices (physical and emulator) with a higher version, it doesn't show image. URL and JSON correct, because program prints message from another JSON field and I tried to debug it for the check.
I read really a lot of posts with a similar problem at StackOverflow and tried all suggestions, mostly concerning using asGif()
. But it doesn't help. Are there any ways to solve this problem?
UPDATE:
Here is example of URL to the GIF image:
http://static.devli.ru/public/images/gifs/201402/90400af2-91c1-4f31-bf5e-b2561b598d7c.gif
And my 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>
答案1
得分: 0
在调试过程中,我发现.listener
调用了onLoadFailed(...)
方法。因此,在那之后,我遇到了一个适当的问题。在Android 7+上,这是由于新的安全配置引起的。
英文:
During debugging I've discovered, that .listener
calls onLoadFailed(...)
method. So, after that, I stumbled to a suitable problem. On Android 7+ it caused because of new security configuration.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论