How to get Image path which is stored by Glide from URL in Android?

旧巷老猫 提交于 2020-07-13 07:18:07

问题


I have 2 activities: SplashActivity and MainActivity. On SplashActivity, I downloaded some Images using Glide with Cache and also added success listener .listener(new RequestListener<Drawable>() { So that I'll get to know that all Images are downloaded and I can move to MainActivity.

Glide.with(this)
                    .load(url)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .listener(new RequestListener<Drawable>() {

In MainActivity, I'm showing these Images in a recyclerview.

Problem: Suppose, user Installed the app very first time. On Splash Screen, I'm checking if user has Internet connection, then Glide will download all the images and store them in a cache. After that it'll move to MainActivity. Now in MainActivity also Internet connection is required to show Images. Why? I already stored in cache.

Please note: The problem only occurs the first time, until the user scrolls past all the images. The second time, if the user opens the app without Internet connection, it works fine. How is that possible?

Why is it not working the first time?

So what I'm thinking is: If I get the Image Path for all Images, then I can set directly inside onBindViewHolder instead of setting URL with Glide.

Can anybody tell me how to get the Image path of Glide stored images?

Adapter:

@Override
    public void onBindViewHolder(PreviewAdapter.MyViewHolder holder, int position) {
        Glide.with(context).load(previewArrayList.get(position).getUrl()).into(holder.postImage);
        holder.postName.setText(previewArrayList.get(position).getTitle());
        holder.postDesc.setText(previewArrayList.get(position).getDesc());
        holder.rootLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                IntentData intentData = new IntentData();
                intentData.post_name = previewArrayList.get(position).getTitle();
                intentData.post_desc = previewArrayList.get(position).getDesc();
                intentData.post_url = previewArrayList.get(position).getUrl();
                intentData.foreign_post_id = previewArrayList.get(position).getForeignPostId();

                Intent intent = new Intent(context, PostActivity.class);
                intent.putExtra(PREVIEW_INTENT_DATA, intentData);
                activity.startActivity(intent);
            }
        });
    }

Please see my previous question: Glide: How to preload Images and get confirmation that it is stored in cache?

See the complete code:

private void preloadImage(String url) {
        try {

           // File file = Glide.with(this).asFile().load(url).submit().get();
           // String path = file.getPath();
            //How can I use above code? in this below logic?

            Glide.with(this)
                    .load(url)
                    .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .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();
        }
    }

来源:https://stackoverflow.com/questions/62619895/how-to-get-image-path-which-is-stored-by-glide-from-url-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!