【问题标题】:How not to make delay while loading image?加载图像时如何不延迟?
【发布时间】:2018-11-18 05:28:57
【问题描述】:

我在 recyclerView 中从服务器加载图像,同时我也在下载该图像。我真正想要做的是,直到图像下载,然后显示带有进度条的图像的模糊或深色不透明预览。加载我使用滑翔的图像并下载我使用Okhttp

要将图像加载到视图中:-

Glide.with(cont).load(modal.get(position).getMassege()).apply(requestOptions).listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, com.bumptech.glide.request.target.Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, com.bumptech.glide.request.target.Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {

                        if(modal.get(position).getProgressPercentage()==0) {
                            new Thread(new Runnable() {
                                @Override
                                public void run() {

                                    downloadFile(modal.get(position).getMassege(), position, modal.get(position).getMsgid(),modal.get(position).getTimeSent());

                                }
                            }).start();
                        }
                        return false;
                    }
                }).into(holder.image);

下载图片

    public void downloadFile(String src, int position, String messageId,String timeStamp) {
    try {
        java.net.URL url = new java.net.URL(src);
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        connection.setDoInput(true);
        connection.connect();
        int lenghtOfFile = connection.getContentLength();
        InputStream input = connection.getInputStream();
        String[] fileNam = src.split("/");
        OutputStream output = new FileOutputStream(cont.getFilesDir() + fileNam[fileNam.length - 1]);
        byte data[] = new byte[1024];

        long total = 0;
        int count = 0;
        int progressPercentage = 0;
        Intent intent = new Intent();
        intent.setAction(Constants.INTENT_FILTER);
        intent.putExtra(Constants.MESSAGE_POSITION_IN_CHAT_LIST, position);
        intent.putExtra(Constants.DOWNLOADING_FILE, true);

        while ((count = input.read(data)) != -1) {
            total += count;
            // publishing the progress....
            // After this onProgressUpdate will be called
            progressPercentage = (int) ((total * 100) / lenghtOfFile);
            if(progressPercentage==30||progressPercentage==60||progressPercentage==100) {
                intent.putExtra(Constants.PROGRESS, progressPercentage);
                intent.putExtra(Constants.FILE_PATH, cont.getFilesDir() + fileNam[fileNam.length - 1]);
                intent.putExtra(Constants.MESSAGE_ID, messageId);
                cont.sendBroadcast(intent);
            }

            // writing data to file
            output.write(data, 0, count);
            Log.d("Downloding" + progressPercentage, "Count" + count);
            if (progressPercentage > 99) {
                DatabaseHelperChattingToUsers databaseHelperChattingToUsers = new DatabaseHelperChattingToUsers(cont);
                String userId = databaseHelperChattingToUsers.getUserExists(sendTo);
                String splits[] = userId.split("/");
                databaseHelperChattingToUsers.updateContact(new ChattingToUsers(splits[0], sendTo, timeStamp, cont.getFilesDir() + fileNam[fileNam.length - 1],"0"));

                DatabaseHelper1 db = new DatabaseHelper1(cont);
                db.updateContact(messageId, "", "", "", 100);
                String filePath = intent.getStringExtra(Constants.FILE_PATH);
                db.updateChatMessage(messageId, filePath);
            }
        }

    } catch (IOException e) {
        e.printStackTrace();

    }
}

我到达的地方:

我能够在 recyclerView 中加载图像时并行下载图像,同时进度条显示图像下载和图像模糊预览。

我面临的问题:

在 recyclerView 中加载图像时图像的模糊预览需要很短的时间,但在 recyclerView 中添加了项目视图并在一段时间后加载图像

【问题讨论】:

标签: java android android-recyclerview android-glide


【解决方案1】:

Glide 会为你下载图片,你为什么要花更多精力通过okHttp 下载它?

下载完整的图像总会有延迟。这取决于您的网络速度和图像大小。

为了减少延迟,您可以在服务器上存储不同大小的图像。在客户端首先下载小尺寸图像并将其显示在recyclerview中。 (更小的图像大小 == 更少的下载时间。)

点击 recyclerview 中的行后,通过点击另一个 url 下载完整的图像。

例如

https://www.example.com/images/small/image1.png ( 250x250 像素 = 100kb )

https://www.example.com/images/medium/image1.png ( 1000x1000 像素 = 400kb )

https://www.example.com/images/large/image1.png (3000x3000 牛 = 1200kb)

【讨论】:

    猜你喜欢
    • 2020-08-12
    • 2023-03-07
    • 2016-12-08
    • 2013-10-24
    • 2021-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    相关资源
    最近更新 更多