Android/Java如何提前获取未来Glide图片大小缓存?

How to priorly get future Glide image size which will be stored in cache in Android/Java?

在 MainActivity 中,我根据图像视图大小使用 glide 将一些图像加载到 recyclerview 中。

参见:

 @Override
    public void onBindViewHolder(PreviewAdapter.MyViewHolder holder, int position) {
        Glide.with(context).load(previewArrayList.get(position).getUrl()).diskCacheStrategy(DiskCacheStrategy.AUTOMATIC).into(holder.postImage);
}

XML:

<ImageView
    android:id="@+id/post_image"
    android:layout_width="match_parent"
    android:layout_marginTop="4dp"
    android:layout_height="250dp"
    android:layout_below="@+id/post_name"
    android:scaleType="centerCrop" />

如您所见,我也在使用 Glide 缓存 DiskCacheStrategy.AUTOMATIC,以便下次没有 Internet Glide 可以显示图像。现在您可以在 post https://medium.com/@multidots/glide-vs-picasso-930eed42b81d 中读到“ Glide resizes the image as per the dimension of the ImageView.

现在,我想要 SpalshActivity 中的最终大小,Glide 将其存储在缓存中。因此,在 SpalshActivity 之后,当用户第一次在没有 Internet Conncetion 的情况下打开 MainActivity 时,它应该加载图像。

那怎么可能呢?

在 SpalshActivity 中,我已经在缓存图像,但它又是 downloading/resizing 在 MainActivity 中第一次。

SpalshActivity:

private void preloadImage(String url) {
        try {

            //File file = Glide.with(this).asFile().load(url).submit().get();
            //String path = file.getPath();


            Glide.with(this)
                    .load(url)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                            if (isPostDataLoaded) {
                                postImagesLoaded++;
                                if (postImagesLoaded == postImagesCount) {
                                    binding.progressBar.setVisibility(View.GONE);
                                    AlertDialogManager.showAlertDialogMessage(SplashActivity.this, "Error", "Something went wrong, Please try again later", false, "Exit", null, SplashActivity.this, IS_TABLET);
                                }
                            } else {
                                previewImagesLoaded++;
                                if (previewImagesLoaded == previewImagesCount) {
                                    binding.progressBar.setVisibility(View.GONE);
                                    AlertDialogManager.showAlertDialogMessage(SplashActivity.this, "Error", "Something went wrong, Please try again later", false, "Exit", null, SplashActivity.this, IS_TABLET);
                                }
                            }
                            return true;
                        }

                        @Override
                        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                            if (isPostDataLoaded) {
                                postImagesLoaded++;
                                if (postImagesLoaded == postImagesCount) {
                                    PostSingleton.getInstance().setPostMap(postMap);
                                    startFreshActivity(PreviewActivity.class);
                                }
                            } else {
                                previewImagesLoaded++;
                                if (previewImagesLoaded == previewImagesCount) {
                                    PreviewSingleton.getInstance().setPreviewList(previewList);
                                    getPostImageCount();
                                    postPreloadAllImages();
                                }
                            }
                            return true;
                        }
                    }).preload();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

最好使用 .downloadOnly() 预加载所有图像,而不是使用任何 target。然后使用 FileProvider.

加载图像
private class CacheImage extends AsyncTask<String,Void,File> {
        @Override
        protected File doInBackground(String... strings) {
            try {
                return Glide.with(getContext())
                        .load(strings[0])
                        .downloadOnly(Target.SIZE_ORIGINAL,Target.SIZE_ORIGINAL)
                        .get();
            } catch (Exception e) {
                Log.e(LOG_TAG,e.getMessage());
                return null;
            }
        }

        @Override
        protected void onPostExecute(File file) {
            if(file!=null){
               Uri file_uri = FileProvider.getUriForFile(getContext(),
                        getContext().getPackageName()+".images",file);
            }
        }
    }

并将路径与 URL 一起存储在 SQLite 中。 现在使用 SQLite

中的 FileProvider 获取 image_url
Glide.with(imageView.getContext())
                .load(<image_url>)
                .asBitmap()
                .dontAnimate()
                .centerCrop()
                .override(<width>,<height>)
                .priority(Priority.IMMEDIATE)
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .skipMemoryCache(true)
                .into(imageView);

您可能还需要添加,

清单中,内部 <application>

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="{app package name}.images"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>

res/xml 内,如 file_paths.xml

<paths xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <cache-path name="images" path="image_manager_disk_cache"
        tools:path="DiskCache.Factory.DEFAULT_DISK_CACHE_DIR" />
</paths>