在 Android 7+ 中通过 Glide 按照 URL 在片段的 ImageView 中显示 GIF 图像。

huangapple go评论111阅读模式
英文:

Show GIF image in fragment ImageView by URL using Glide on Android 7+

问题

以下是您提供的代码的翻译部分:

  1. public class FragmentHandler extends Fragment implements View.OnClickListener {
  2. private void showJsonContent(JSONObject jsonObject) throws JSONException {
  3. if (jsonObject == null) return;
  4. String gifLink = jsonObject.getString("gifURL");
  5. String desc = jsonObject.getString("description");
  6. captionTextView.setText(desc);
  7. Glide.with(Objects.requireNonNull(getActivity())
  8. .getApplicationContext())
  9. .asGif()
  10. .load(gifLink)
  11. .listener(new RequestListener<GifDrawable>() {
  12. @Override
  13. public boolean onLoadFailed(GlideException e, Object model, Target<GifDrawable> target, boolean isFirstResource) {
  14. progressBar.setVisibility(View.GONE);
  15. return false;
  16. }
  17. @Override
  18. public boolean onResourceReady(GifDrawable resource, Object model, Target<GifDrawable> target, DataSource dataSource, boolean isFirstResource) {
  19. progressBar.setVisibility(View.GONE);
  20. return false;
  21. }
  22. }).into(new ImageViewTarget<GifDrawable>(imageView) {
  23. @Override
  24. protected void setResource(GifDrawable resource) {
  25. imageView.setImageDrawable(resource);
  26. }
  27. });
  28. }
  29. }
  1. <ProgressBar
  2. android:id="@+id/progress"
  3. android:layout_width="150dp"
  4. android:layout_height="150dp"
  5. android:layout_centerVertical="true"
  6. android:layout_centerHorizontal="true"
  7. android:visibility="visible" />
  8. <ImageView
  9. android:id="@+id/imageView"
  10. android:layout_width="240dp"
  11. android:layout_height="240dp"
  12. android:layout_centerVertical="true"
  13. android:layout_centerHorizontal="true"
  14. android:contentDescription="@string/gif_image" />

更新:
以下是GIF图像的示例URL:

  1. http://static.devli.ru/public/images/gifs/201402/90400af2-91c1-4f31-bf5e-b2561b598d7c.gif

以及我的AndroidManifest:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.example.gifapp">
  4. <uses-feature android:name="android.hardware.wifi" android:required="true" />
  5. <uses-permission android:name="android.permission.INTERNET"/>
  6. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  7. <application
  8. android:allowBackup="true"
  9. android:fullBackupContent="@xml/my_backup_rules"
  10. android:icon="@mipmap/ic_launcher"
  11. android:label="@string/app_name"
  12. android:roundIcon="@mipmap/ic_launcher_round"
  13. android:supportsRtl="true"
  14. android:theme="@style/AppTheme">
  15. <activity android:name=".MainActivity">
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN" />
  18. <category android:name="android.intent.category.LAUNCHER" />
  19. </intent-filter>
  20. </activity>
  21. </application>
  22. </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.

  1. public class FragmentHandler extends Fragment implements View.OnClickListener {
  2. private void showJsonContent(JSONObject jsonObject) throws JSONException {
  3. if (jsonObject == null) return;
  4. String gifLink = jsonObject.getString(&quot;gifURL&quot;);
  5. String desc = jsonObject.getString(&quot;description&quot;);
  6. captionTextView.setText(desc);
  7. Glide.with(Objects.requireNonNull(getActivity())
  8. .getApplicationContext())
  9. .asGif()
  10. .load(gifLink)
  11. .listener(new RequestListener&lt;GifDrawable&gt;() {
  12. @Override
  13. public boolean onLoadFailed(GlideException e, Object model, Target&lt;GifDrawable&gt; target, boolean isFirstResource) {
  14. progressBar.setVisibility(View.GONE);
  15. return false;
  16. }
  17. @Override
  18. public boolean onResourceReady(GifDrawable resource, Object model, Target&lt;GifDrawable&gt; target, DataSource dataSource, boolean isFirstResource) {
  19. progressBar.setVisibility(View.GONE);
  20. return false;
  21. }
  22. }).into(new ImageViewTarget&lt;GifDrawable&gt;(imageView) {
  23. @Override
  24. protected void setResource(GifDrawable resource) {
  25. imageView.setImageDrawable(resource);
  26. }
  27. });
  28. }
  29. }

Progress Bar and ImageView in XML looks like that:

  1. &lt;ProgressBar
  2. android:id=&quot;@+id/progress&quot;
  3. android:layout_width=&quot;150dp&quot;
  4. android:layout_height=&quot;150dp&quot;
  5. android:layout_centerVertical=&quot;true&quot;
  6. android:layout_centerHorizontal=&quot;true&quot;
  7. android:visibility=&quot;visible&quot; /&gt;
  8. &lt;ImageView
  9. android:id=&quot;@+id/imageView&quot;
  10. android:layout_width=&quot;240dp&quot;
  11. android:layout_height=&quot;240dp&quot;
  12. android:layout_centerVertical=&quot;true&quot;
  13. android:layout_centerHorizontal=&quot;true&quot;
  14. android:contentDescription=&quot;@string/gif_image&quot; /&gt;

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:

  1. http://static.devli.ru/public/images/gifs/201402/90400af2-91c1-4f31-bf5e-b2561b598d7c.gif

And my AndroidManifest:

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. package=&quot;com.example.gifapp&quot;&gt;
  4. &lt;uses-feature android:name=&quot;android.hardware.wifi&quot; android:required=&quot;true&quot; /&gt;
  5. &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot;/&gt;
  6. &lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot;/&gt;
  7. &lt;application
  8. android:allowBackup=&quot;true&quot;
  9. android:fullBackupContent=&quot;@xml/my_backup_rules&quot;
  10. android:icon=&quot;@mipmap/ic_launcher&quot;
  11. android:label=&quot;@string/app_name&quot;
  12. android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
  13. android:supportsRtl=&quot;true&quot;
  14. android:theme=&quot;@style/AppTheme&quot;&gt;
  15. &lt;activity android:name=&quot;.MainActivity&quot;&gt;
  16. &lt;intent-filter&gt;
  17. &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
  18. &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
  19. &lt;/intent-filter&gt;
  20. &lt;/activity&gt;
  21. &lt;/application&gt;
  22. &lt;/manifest&gt;

答案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.

huangapple
  • 本文由 发表于 2020年8月30日 03:19:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63650896.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定