【问题标题】:How to fetch the image from server and show into the listview android?如何从服务器获取图像并显示到 listview android 中?
【发布时间】:2015-07-25 19:53:13
【问题描述】:

我是 android 的初学者,我想编写一个简单的应用程序来从服务器读取图像和数据并显示到列表视图中,我的列表视图 xml 文件是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_gravity="center"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:src="@drawable/abc_btn_check_material" />

    <TextView
        android:id="@+id/textView1"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="TextView" />

</LinearLayout>

并使用此方法从服务器读取 json 数据:

....READ WITH ASYNCTASK
try {
                JSONArray jsonArray=new JSONArray(result);
                prgmNameList=new String[jsonArray.length()];
                prgmImages=new int[jsonArray.length()];

                for(int i=0;i<jsonArray.length();i++)
                {
                    JSONObject JsonObj=jsonArray.getJSONObject(i);
                    String TITLES=JsonObj.getString("title");
                    String imgURL=JsonObj.getString("img");
                    Log.d("BEHZAD TITLES=",TITLES);
                    .....I want read the image and show into the listview with title
}


我该如何解决这个计划?谢谢。

【问题讨论】:

  • URL newurl = new URL(photo_url_str); mIcon_val = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream()); profile_photo.setImageBitmap(mIcon_val); 在这里回答stackoverflow.com/questions/2471935/…

标签: android listview


【解决方案1】:

【讨论】:

    【解决方案2】:

    您好,您可以创建一个 BitmapDownloaderTask 类来下载您的图像。

    public class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
    
    
        private String url;
        private String path, imgName;
        private final WeakReference<ImageView> imageViewReference;
    
    
        public BitmapDownloaderTask(ImageView imageView, String path, String imgName) {
    
            imageViewReference = new WeakReference<ImageView>(imageView);
    
            this.path = path;
            this.imgName = imgName;
    
        }//BitmapDownloaderTask
    
    
        @Override
        // Actual download method, run in the task thread
        protected Bitmap doInBackground(String... params) {
            // params comes from the execute() call: params[0] is the url.
            return getBitmapFromURL(params[0]);
    
        }//doInBackground
    
    
        @Override
        // Once the image is downloaded, associates it to the imageView
        protected void onPostExecute(Bitmap bitmap) {
    
            if (isCancelled()) {
                bitmap = null;
            }
    
            if (imageViewReference != null && bitmap != null) {
                ImageView imageView = imageViewReference.get();
                if (imageView != null) {
                    imageView.setImageBitmap(bitmap);
    
                    //this is my custom method to save bitmap in the local storage
                    Utility.saveBitmapToLocalStorage(bitmap, this.path, this.imgName);
                }
            }
    
        }//onPostExecute
    
    
        public static Bitmap getBitmapFromURL(String link) {
            /* this method downloads an Image from the given URL,
            *  then decodes and returns a Bitmap object
            */
            try {
                URL url = new URL(link);
                HttpURLConnection connection = (HttpURLConnection)url.openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                Bitmap myBitmap = BitmapFactory.decodeStream(input);
    
                return myBitmap;
    
            } catch (IOException e) {
                e.printStackTrace();
                Log.e("getBmpFromUrl error: ", e.getMessage().toString());
                return null;
            }
    
        }//getBitmapFromURL
    
    
    }//class
    

    您可以像这样正常创建您的 ListView(使用自定义适配器)

    String[] strings = new String[]{"item 1","item 2","item 3"};
    
    ListView mList = (ListView)rootView.findViewById(R.id.mListId);
    
    MAdapter adapter = new MAdapter(this, R.layout.adapter_listview_mAdapter, strings);
    mList.setAdapter(adapter);
    

    最后在 MAdapter 类中你可以使用你的 BitmapDownloaderTask 来下载你的图片

    public class MAdapter extends ArrayAdapter<YourModel> {
    
    
        public MAdapter(Context context, int resource, List<YourModel> items) {
    
            super(context, resource, items);
    
        }//MAdapter
    
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.adapter_listview_mAdapter, null);
    
    
            ImageView image = (ImageView)convertView.findViewById(R.id.imageId);
    
            YourModel model = getItem(position);
    
            BitmapDownloaderTask task = new BitmapDownloaderTask(image, "path", "folder_" + model.identifier);
                task.execute(url);
            }
    
            return convertView;
    
        }//getView
    
    
    }//class
    

    【讨论】:

      【解决方案3】:

      这很简单,只需创建一个 URLConnection 并为您的列表视图调整大小。所有代码都可以在这里找到:Best method to download image from url in Android。希望我能帮到你;)

      【讨论】:

        【解决方案4】:

        这里描述了如何创建自定义适配器和显示图像:

        Using Picasso library with ListView

        你可以去看看另一个很棒的图书馆Glide而不是毕加索

        他们都更容易使用通用图像加载器

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-21
          • 2019-05-25
          • 1970-01-01
          相关资源
          最近更新 更多