如何预先获取将来的Glide图像大小,该图像大小将存储在Android/Java中的缓存中? [英] How to priorly get future Glide image size which will be stored in cache in Android/Java?

查看:76
本文介绍了如何预先获取将来的Glide图像大小,该图像大小将存储在Android/Java中的缓存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MainActivity中,我根据图像视图的大小使用滑行将一些图像加载到recyclerview中.

In MainActivity, I'm loading some images using glide into recyclerview according to imageview size.

请参阅:

 @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的时候可以显示图像.现在,您可以在这篇文章中阅读 https://medium.com/@multidots/glide -vs-picasso-930eed42b81d Glide resizes the image as per the dimension of the ImageView. ""

As you can observe, I'm using Glide cache DiskCacheStrategy.AUTOMATIC also so that next time without Internet Glide can show the images. Now you can read in this post https://medium.com/@multidots/glide-vs-picasso-930eed42b81d that " Glide resizes the image as per the dimension of the ImageView."

现在,我想要SpalshActivity中的最终大小,Glide将存储在缓存中.这样,在SpalshActivity之后,当用户也第一次打开MainActivity 没有Internet连接时,它就应该加载图像.

Now, I want that final size inside SpalshActivity, which Glide will be stored in cache. So that when After SpalshActivity, when user opens MainActivity without Internet Conncetion for the very first time also, then it should load Images.

那怎么可能?

在SpalshActivity中,我已经在缓存图像,但是它是第一次在MainActivity中下载/调整大小.

In SpalshActivity, I'm already caching Images, but it is again downloading/resizing in MainActivity for the very first time.

SpalshActivity:

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加载图像.

Better to preload all the images with .downloadOnly() instead of using any target. Then load images using 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

And store the path alongside URL in SQLite. Now get the image_url using FileProvider from SQLite

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>

这篇关于如何预先获取将来的Glide图像大小,该图像大小将存储在Android/Java中的缓存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆