PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 17, 2022

[FIXED] how to dowload and load .gif in android using volley?

 July 17, 2022     android, android-json, android-volley, gif     No comments   

Issue

i am using this gif library https://github.com/koral--/android-gif-drawable to display gif in xml and gifImageView as view , i was using ImageRequest in volley to download gif from url . but the gif is shown as still image in app. cant figure out what happened ?pls help me with my code or pls tell me how to download gif via volley.

variables defined:-

SwipeDeck cardSwipe;
String url = "";
ImageView iv;
RequestQueue que;
RequestQueue que2;
SwipeCardAdapter adapter;
ArrayList<Bitmap> urls = new ArrayList<>();

this is stringRequest function block to fetch Json

public StringRequest strReq(){

    StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                JSONArray dataObject = jsonObject.getJSONArray("data");

                for(int i = 0 ; i < dataObject.length() ; i++){
                    JSONObject thisObject = dataObject.getJSONObject(i);
                    JSONObject image = thisObject.getJSONObject("images");
                    JSONObject imageType = image.getJSONObject("original");
                    String imageUrl = imageType.getString("url");
                    Log.d("TAG", imageUrl);
                    URL url = new URL(imageUrl);
                    que2.add(imageReq(imageUrl));

                }

            } catch (JSONException e) {
                e.printStackTrace();
                Log.d("TAG","error occured in try and catch");

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

            if(error.getCause() != null){
                error.getCause().printStackTrace();
            }
            if(error.networkResponse != null){
                Toast.makeText(MainActivity.this, "Network Response is " + error.networkResponse , Toast.LENGTH_SHORT).show();
            }

        }
    });


            return stringRequest;
}

and this is the imageRequest function block to download gif from the url:-

public ImageRequest imageReq(String url){

        ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {

                urls.add(response);

                adapter.notifyDataSetChanged();



            }
        }, 0, 0, null, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                if(error.getCause() != null){
                    error.getCause().printStackTrace();
                }
                if(error.networkResponse != null){
                    Toast.makeText(MainActivity.this, "Network Response is " + error.networkResponse , Toast.LENGTH_SHORT).show();
                }

            }
        });
        return imageRequest;
    }

and this inside the adapter class to set gifImageView:-

GifImageView gifImageView = (GifImageView) convertView.findViewById(R.id.gifIV);


            Bitmap thisGif = getItem(position);

            gifImageView.setImageBitmap(thisGif);

Solution

I am not exactly sure why you are using the GifDrawable Library the Glide Library is so much better. https://github.com/bumptech/glide

Glide.with(mContext).load(image.getMedium())
                .thumbnail(0.5f)
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(holder.thumbnail);


Answered By - dominicd
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing