Android将图像设置为来自JSON url的imageview

时间:2018-05-02 23:36:11

标签: android json url

 protected void onPostExecute(String s) {
            super.onPostExecute(s);
            //System.out.println("alınan data:" + s);

            try{
                JSONObject jsonObject = new JSONObject(s);


                String poster = jsonObject.getString("Poster");

                InputStream is = (InputStream) new URL(poster).getContent();
                Drawable d = Drawable.createFromStream(is, "src name");
                Poster.setImageDrawable(d);


                String Title = jsonObject.getString("Title");
                tryText.setText("Title: " + Title);

                String Year = jsonObject.getString("Year");
                movieYear.setText("Year: " + Year);



            }catch (Exception e){

            }
        }

我想显示来自JSONObject网址的图片,但是找不到方法。有一个EditText,它会在点击按钮时动态更改URL。

1 个答案:

答案 0 :(得分:0)

你可以试试这个

  Bitmap image = null;
  try {
    InputStream in = new URL(poster).openStream();
    image = BitmapFactory.decodeStream(in);
  } catch (Exception e) {
      Log.e("Error", e.getMessage());
      e.printStackTrace();
  }
 if(image!=null)
      imageView.setImageBitmap(image);