1

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>
po4yka
  • 151
  • 2
  • 7
  • Please add an example of your URL as well as your manifest – Mouaad Abdelghafour AITALI Aug 29 '20 at 19:42
  • @MouaadAbdelghafourAITALI I updated the information in question – po4yka Aug 29 '20 at 19:47
  • I've tested your code on my phone (running Android 8.0) and everything works fine, make sure you have a good internet connection and try again, or test with local file, instead of using `.load(gifLink)` use `.load(R.drawable.your_gif)` – Mouaad Abdelghafour AITALI Aug 29 '20 at 19:54
  • However, I don't know why you use all this code `into(new ImageViewTarget ....` to set gifDrawable you can simply set it like this `.into(imageView)` – Mouaad Abdelghafour AITALI Aug 29 '20 at 20:02
  • @MouaadAbdelghafourAITALI I tried to use a local GIF image and everything works. But with URL (also with hardcoded) it doesn't show. I checked my internet connection (tried Wi-Fi and mobile), but this did not change the situation in any way. I used `into(new ImageViewTarget ....` construction because it was one of possible solution of the problem... Never changes with `.into(imageView)` – po4yka Aug 29 '20 at 20:08
  • Did you try to reinstall your app, maybe something went wrong with the cache – Mouaad Abdelghafour AITALI Aug 29 '20 at 20:10
  • @MouaadAbdelghafourAITALI Yeah, on a physical device and several emulators – po4yka Aug 29 '20 at 20:14
  • 1
    weird issue,if nothing works for you, try to get the gifDrawable using Glide and set it on gifImageView using [Android GIF Drawable](https://github.com/koral--/android-gif-drawable) library – Mouaad Abdelghafour AITALI Aug 29 '20 at 20:19

1 Answers1

0

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.

po4yka
  • 151
  • 2
  • 7