使用 Picasso 和 Volley 在片段图像视图上显示 URL 图像
Display URL image on fragment image view using Picasso and Volley
我能够使用 Volley 在 textview 上加载 JSON 数据,但图像由 Picasso 处理以将其加载到 ImageView
。这是我的代码:
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);
}
这是我使用 Volley
从 URL 加载图像的方法
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();
}
}
从 stringrequest
加载的文本在 Volley 的文本视图上完美加载,但只有图像没有加载。
我已经在主要 activity 上尝试过这种方法,并且有效。 但是为什么片段部分没有加载呢?
这样做
Picasso.with(getContext())
.load(jsonImage)
.into(imageFromURL);
我能够使用 Volley 在 textview 上加载 JSON 数据,但图像由 Picasso 处理以将其加载到 ImageView
。这是我的代码:
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);
}
这是我使用 Volley
从 URL 加载图像的方法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();
}
}
从 stringrequest
加载的文本在 Volley 的文本视图上完美加载,但只有图像没有加载。
我已经在主要 activity 上尝试过这种方法,并且有效。 但是为什么片段部分没有加载呢?
这样做
Picasso.with(getContext())
.load(jsonImage)
.into(imageFromURL);