【问题标题】:Images from server not showing in Android Custom ListView来自服务器的图像未显示在 Android 自定义 ListView 中
【发布时间】:2018-08-17 11:48:56
【问题描述】:

我想将来自服务器的图像显示到 Android 应用程序的自定义列表视图中,但无法正常工作。

我有这个来自服务器的 json 数组。 {"result":[{"url":"https:\/\/augmentedandroidapp.000webhostapp.com\/images\/kamara1.jpg"},{"url":"https:\/\/augmentedandroidapp.000webhostapp.com\/images\/kamara2.jpg"},{"url":"https:\/\/augmentedandroidapp.000webhostapp.com\/images\/kamara3.jpg"}]}

我成功地将 url 提取到一个新的 JSON 对象中,并通过下面的 getImage 方法将图像 url 转换为位图。根据Log结果,转换为位图似乎是对的。

public class Getjson {
public static String[] Image_Url;
public static Bitmap[] bitmaps;
Activity context;
public static final String JSON_ARRAY="result";
public static final String IMAGEURL = "url";
private String json;
private JSONArray urls;
public Getjson(String json){
    this.json = json;
    try {
        JSONObject jsonObject = new JSONObject(json);
        //urls = new JSONArray (jsonObject.getString("results"));
        urls = jsonObject.getJSONArray(JSON_ARRAY);
        Log.e("GalleryTargets", "Url" + urls);
        } catch (JSONException e) {
        e.printStackTrace();
        Log.e(LOGTAG, "No Url" + urls);

    }
}
private Bitmap getImage(JSONObject jo){
    URL url = null;
    Bitmap image = null;
    try {
        url = new URL(jo.getString(IMAGEURL));
        image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
        Log.e(LOGTAG, "Bitmap0" + image);

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return image;
}
public void getAllImages() throws JSONException {
    Image_Url = new String[urls.length()];
    bitmaps = new Bitmap[urls.length()];
    for(int i=0;i<urls.length();i++)
    {   Image_Url[i] = urls.getJSONObject(i).getString(IMAGEURL);
        Log.e(LOGTAG, "Bitmap1" + Image_Url[i]);
        JSONObject jsonObject = urls.getJSONObject(i);
        bitmaps[i]=getImage(jsonObject);
        Log.e(LOGTAG, "Bitmap2" + bitmaps[i]);

    }
}

}

现在,使用下面的方法,我想在自定义 ListView 中显示图像,但图像没有显示。任何想法为什么会发生这种情况?下面的 CustomAdapter & mainActivity 任务正常运行但不显示图像..

提前谢谢你

public class Customadapter extends ArrayAdapter<String> {
private String[] urls;
private Bitmap[] bitmaps;
private Activity context;
public Customadapter(Activity context, Bitmap[] bitmaps  ) {
    super(context, R.layout.gallery_list);
    this.context = context;
    this.bitmaps = bitmaps;
    Log.e("Custom", "bitmaps" + bitmaps);

}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View listViewItem = inflater.inflate(R.layout.gallery_list, parent, true);
    ImageView image = (ImageView) listViewItem.findViewById(R.id.imgvw);
    image.setImageBitmap(Bitmap.createScaledBitmap(bitmaps[position], 100, 100, false));
    Log.e("Custom", "bitmaps" + bitmaps[position]);
    return  listViewItem;
}}

//和mainActivity任务

 public Getjson getjsonobj;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.myListView);
    listView = (ListView) findViewById(R.id.listView);
    getURLs();
}
private void getImages() {
    class GetImages extends AsyncTask<Void, Void, Void> {
        ProgressDialog loading;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(GalleryTargets.this, "Loading Images", "Please wait...", false, false);
        }

        @Override
        protected void onPostExecute(Void v) {
            super.onPostExecute(v);
            loading.dismiss();
            customadapter = new Customadapter(MainActivity.this, getjsonobj.bitmaps);
            Log.e("Custom2", "bitmaps" + getjsonobj.bitmaps);
            listView.setAdapter(customadapter);
        }

        @Override
        protected Void doInBackground(Void... voids) {
            try {
                getjsonobj.getAllImages();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }
    }
    GetImages getImages = new GetImages();
    getImages.execute();    }

【问题讨论】:

  • 为什么不使用 Picasso 或 Glide 库进行图像加载?

标签: android android-layout listview android-studio


【解决方案1】:

您可以使用 Picasso 或 Glide 直接在 Imageview 中加载您的图像。 例如:

Picasso.with(context).load("您的图片网址").into(您的图片浏览);

这是从服务器加载 imageview 中图像的简单方法。

【讨论】:

  • 我不想使用特定的网址。但我想动态地采用它们,而不是作为字符串。可以通过毕加索吗?谢谢!
  • 你从 json 中获取你的图片 url,比如 urls = jsonObject.getJSONArray(JSON_ARRAY);这是您的图片网址。只是放入毕加索
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多