java - Jsoup读取图像速度很慢

标签 java android jsoup

我想从网站下载图像并将其显示在列表中,但下载速度很慢我认为我的代码有问题。

私有(private)类 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);

    }
}

最佳答案

这是因为每次找到 src 时,您都会尝试将图像源解码为位图:

InputStream input = new java.net.URL(imgSrc).openStream();
// Decode Bitmap
bitmap = BitmapFactory.decodeStream(input);

将输入流解码为位图是一个昂贵且缓慢的过程。

然后您将一直等待并累积解码图像中所有图像所需的时间:

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

要解决这个问题,您只需要保存图像网址并稍后使用如下代码对其进行解码:

// 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;
}

然后使用 Glide 或 Picasso 等解码 RecyclerView Adapter 中的图像。

关于java - Jsoup读取图像速度很慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55983248/

相关文章:

java - 函数式 java 8 与传统 java

java - 缺少有关 Google Translation API 客户端问题的有效 API key ?

java - 如何让多个 Activity 使用相同的跟踪 Activity ?

java - 安卓闹钟问题

java - 尝试访问 Login 元素,但没有元素 id 或类

java - 将类扩展到库父类(super class)

java - 在这些情况下会发生死锁吗?

java - 如何在java中初始化(int,string)的有序对

android - 图形 API 在 Android 中无法正常工作

在 Java 中使用 Jsoup 时出现 java.lang.IllegalArgumentException