[英]Jsoup speed very slow in reading images
I want to download images from the website and display them in the list but the speed of the downloading is very slow I assume there is a problem in my code. 我想从网站下载图像并将其显示在列表中,但下载速度非常慢我认为我的代码存在问题。
private class mAsyncTask extends AsyncTask { 私有类mAsyncTask扩展AsyncTask {
Bitmap bitmap;
@Override
protected Void doInBackground(Void... params) {
try {
// Connect to the web site
Document document = Jsoup.connect(url).get();
// Using Elements to get the class data
Elements mElementDataSize1 = document.select("h5");
for (int i = 0; i < mElementDataSize1.size(); i++) {
Elements img = document.select("[class=product_list grid row] img[src]").eq(i);
// Locate the src attribute
String imgSrc = img.attr("src");
// Download image from URL
InputStream input = new java.net.URL(imgSrc).openStream();
// Decode Bitmap
bitmap = BitmapFactory.decodeStream(input);
bitmapArray.add(bitmap); // Add a bitmap
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mAdapter mDataAdapter = new mAdapter(MainActivity.this,bitmapArray);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(getApplicationContext(), 2);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mDataAdapter);
}
}
It's because you're trying to decode the image source to bitmap each times you found the src
with: 这是因为你每次找到src
时都试图将图像源解码为位图:
InputStream input = new java.net.URL(imgSrc).openStream();
// Decode Bitmap
bitmap = BitmapFactory.decodeStream(input);
decoding an input stream into a bitmap is an expensive process and slow. 将输入流解码为位图是一个昂贵的过程并且很慢。
And then you'll always waiting and accumulated the time needed to decode an image for all the images in: 然后,您将始终等待并累积解码所有图像的图像所需的时间:
for (int i = 0; i < mElementDataSize1.size(); i++) {
...
String imgSrc = img.attr("src");
// You accumulate the the time needed to decode the image here.
bitmap = BitmapFactory.decodeStream(input);
...
}
To solve the problem, you need to only save the images url and decode them later with something like this: 要解决此问题,您只需要保存图像URL并稍后解码它们,如下所示:
// save the images url instead of the Bitmap.
private List<String> mImageUrls = new ArrayList<>();
@Override
protected Void doInBackground(Void... params) {
try {
// Connect to the web site
Document document = Jsoup.connect(url).get();
// Using Elements to get the class data
Elements mElementDataSize1 = document.select("h5");
for (int i = 0; i < mElementDataSize1.size(); i++) {
Elements img = document.select("[class=product_list grid row] img[src]").eq(i);
// Locate the src attribute
String imgSrc = img.attr("src");
// add the url
mImageUrls.add(imgSrc);
}
} catch (IOException e) {
e.printStackTrace();
}
return v;
}
then use Glide or Picasso or etc to decode the images in your RecyclerView Adapter. 然后使用Glide或Picasso等解码RecyclerView适配器中的图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.