如何在Recycler视图中从Url加载图像

我是android新手,我想知道如何在Recycler视图中从URL加载图像,并轻松定义从URL加载图像时Bitmap及其function的用途。 我不想使用任何第三方库。 请为此提供解决方案。

您的问题似乎有点不清楚要么您要求使用URL或其他任何内容阅读图像。 如果是这样,您可以像这样读取图像并将其设置为ImageView

 InputStream in = (InputStream) new URL(imageUrl).getContent(); //Reads whatever content found with the given URL Asynchronously And returns. Bitmap bitmap = BitmapFactory.decodeStream(in); //Decodes the stream returned from getContent and converts It into a Bitmap Format yourImageView.setBitmap(bitmap); //Sets the Bitmap to ImageView in.close(); //Closes the InputStream 

其中imageUrl是一个完整的URL,指向您的图像,如something.com/image1.jpg

我想你已经有了字符串URL。 请参考此链接位图链接此处此方法可以在onBindViewHolder()中声明。

正如他们所说的那样,你应该使用第三方库。 您可以在这里选择它们https://android-arsenal.com/

我会推荐Glide: http : //inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en

我找到了问题的答案,我使用Asynctask加载多个URL来从url加载图像,而不使用第三方libarires。 下载发生在后台线程中。 这减少了主UI线程上的工作,并在后台线程上并行运行下载运行,并且位图的结果在onPostExecute()方法上传递。该方法最终可以在OnBindViewHolder上传递,以将图像绑定到其各自的视图。