来自服务器的图像未显示在Android Custom ListView中

时间:2018-03-09 00:24:23

标签: android android-layout listview android-studio

我想在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转换为位图。根据日志结果,转换为位图似乎是正确的。

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&amp;下面的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 Task

 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();    }

1 个答案:

答案 0 :(得分:0)

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

Picasso.with(context).load(“您的图片网址”)。进入(您的IMAGEVIEW );

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