簡體   English   中英

圖像不會從 web 加載

[英]Image won't load from web

我正在嘗試顯示來自另一個站點的隨機 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;
    }
}

這是這段代碼的結果在此處輸入圖像描述

使用加載圖像服務,如 glide、fresco、picasso

千倍比這種方式更容易和更快

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM