Search code examples
javaandroidandroid-fragmentsandroid-volleypicasso

Display URL image on fragment image view using Picasso and Volley


I was able to load JSON data on textview using Volley but image was handled by Picasso to load it into an ImageView. Here's my code:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_home, container, false);

    Context c = getActivity().getApplicationContext();
    tvQuote = (TextView) v.findViewById(R.id.quote);
    tvAuthor = (TextView) v.findViewById(R.id.author);

    Typeface MP = Typeface.createFromAsset(getActivity().getAssets(), "font/MP.ttf");

    tvQuote.setTypeface(MP);
    tvAuthor.setTypeface(MP);

    imageFromURL=(ImageView)v. findViewById(R.id.imgUrl);


    getQuote();
    loadImageFromURL();
    //Picasso.with(getActivity().getApplicationContext()).load(jsonImage).into(imageFromURL);

    Log.d("Picasso","Error");

    return v;
    //return inflater.inflate(R.layout.fragment_home, container, false);
}

Here's my method for loading image from a URL using Volley

private void loadImageFromURL() {

    List<Integer> imageList = new ArrayList<Integer>();
    imageList.add(R.drawable.ic_action_loader);

    //swipeRefreshLayout.setRefreshing(true);
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
            imgURL, (String) null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {

            try {
                JSONObject urls = response.getJSONObject("urls");
                String regular = urls.getString("regular");

                JSONObject user = response.getJSONObject("user");
                String name = user.getString("name");
                String username = user.getString("username");

                jsonImage = regular;
                jsonName = name;
                jsonUserName = username;

                Picasso.with(getActivity().getApplicationContext())
                        .load(jsonImage)
                        //.placeholder(R.drawable.desert)
                        .into(imageFromURL);

                Log.d(TAG, jsonImage);
                tvPhotographer.setClickable(true);
                tvPhotographer.setMovementMethod(LinkMovementMethod.getInstance());
                String authorLink = "Photo By - <a href='https://unsplash.com/@" + jsonUserName + "'>" + jsonName + "</a> / <a href='https://unsplash.com/'>Unsplash</a>";

                Spanned result;

                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                    result = Html.fromHtml(authorLink, Html.FROM_HTML_MODE_LEGACY);
                } else {
                    result = Html.fromHtml(authorLink);
                }

                tvPhotographer.setText(result);

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
        }
    });

    // Adding request to request queue
    jsonObjReq.setTag(TAG);

    if (mInstance != null) {
        // Add a request to your RequestQueue.
        mInstance.addToRequestQueue(jsonObjReq);
        // Start the queue
        mInstance.getRequestQueue().start();
    }
}

Texts which are loaded from stringrequest are loaded perfectly on the textviews with Volley, but only the image is not loaded.

I have tried this method on the main activity and it works. But why isn't it loaded on the fragment part?


Solution

  • do it like this

    Picasso.with(getContext())
                            .load(jsonImage)
                            .into(imageFromURL);