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

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

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(&quot;gifURL&quot;);
			String desc = jsonObject.getString(&quot;description&quot;);
			captionTextView.setText(desc);
			Glide.with(Objects.requireNonNull(getActivity())
					.getApplicationContext())
					.asGif()
					.load(gifLink)
					.listener(new RequestListener&lt;GifDrawable&gt;() {

						@Override
						public boolean onLoadFailed(GlideException e, Object model, Target&lt;GifDrawable&gt; target, boolean isFirstResource) {
							progressBar.setVisibility(View.GONE);
							return false;
						}

						@Override
						public boolean onResourceReady(GifDrawable resource, Object model, Target&lt;GifDrawable&gt; target, DataSource dataSource, boolean isFirstResource) {
							progressBar.setVisibility(View.GONE);
							return false;
						}

					}).into(new ImageViewTarget&lt;GifDrawable&gt;(imageView) {
						@Override
						protected void setResource(GifDrawable resource) {
							imageView.setImageDrawable(resource);
						}
					});
    }
}

Progress Bar and ImageView in XML looks like that:

&lt;ProgressBar
    android:id=&quot;@+id/progress&quot;
    android:layout_width=&quot;150dp&quot;
    android:layout_height=&quot;150dp&quot;
    android:layout_centerVertical=&quot;true&quot;
    android:layout_centerHorizontal=&quot;true&quot;
    android:visibility=&quot;visible&quot; /&gt;

&lt;ImageView
    android:id=&quot;@+id/imageView&quot;
    android:layout_width=&quot;240dp&quot;
    android:layout_height=&quot;240dp&quot;
    android:layout_centerVertical=&quot;true&quot;
    android:layout_centerHorizontal=&quot;true&quot;
    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:

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

And my AndroidManifest:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;manifest xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    package=&quot;com.example.gifapp&quot;&gt;

    &lt;uses-feature android:name=&quot;android.hardware.wifi&quot; android:required=&quot;true&quot; /&gt;

    &lt;uses-permission android:name=&quot;android.permission.INTERNET&quot;/&gt;
    &lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot;/&gt;

    &lt;application
        android:allowBackup=&quot;true&quot;
        android:fullBackupContent=&quot;@xml/my_backup_rules&quot;
        android:icon=&quot;@mipmap/ic_launcher&quot;
        android:label=&quot;@string/app_name&quot;
        android:roundIcon=&quot;@mipmap/ic_launcher_round&quot;
        android:supportsRtl=&quot;true&quot;
        android:theme=&quot;@style/AppTheme&quot;&gt;
        &lt;activity android:name=&quot;.MainActivity&quot;&gt;
            &lt;intent-filter&gt;
                &lt;action android:name=&quot;android.intent.action.MAIN&quot; /&gt;
                &lt;category android:name=&quot;android.intent.category.LAUNCHER&quot; /&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
    &lt;/application&gt;

&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:

确定