[英]Image won't load from web
I'm trying to display an image from a random URL which I get from another site, but the image doesn't display when run, this is the code I tried (imageView is declared above onCreate and parse is called in onCreate).我正在尝试显示来自另一个站点的随机 URL 的图像,但是运行时图像不显示,这是我尝试过的代码(imageView 在 onCreate 上面声明,并且在 onCreate 中调用了 parse )。
void parse(String url){
final RequestQueue requestQueue = Volley.newRequestQueue(cat.this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
String message;
@Override
public void onResponse(JSONObject response) {
try{
message=response.getString("url");
imageView.setImageBitmap(getBitmap(message));
}
catch (JSONException e){ e.printStackTrace(); }
}
}, error -> {
error.printStackTrace();
requestQueue.stop();
});
requestQueue.add(jsonObjectRequest);
requestQueue.stop();
}
public Bitmap getBitmap(String src) {
try {
java.net.URL url = new java.net.URL(src);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
use loading image services like glide,fresco,picasso使用加载图像服务,如 glide、fresco、picasso
thousand time easier and faster than this way千倍比这种方式更容易和更快
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.