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

前端 未结 1 673
囚心锁ツ
囚心锁ツ 2020-12-12 08:11

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

See:

 @Override
    public void onBindViewHolder(P         


        
1条回答
  •  遥遥无期
    2020-12-12 08:33

    Better to preload all the images with .downloadOnly() instead of using any target. Then load images using FileProvider.

    private class CacheImage extends AsyncTask {
            @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);
                }
            }
        }
    

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

    Glide.with(imageView.getContext())
                    .load()
                    .asBitmap()
                    .dontAnimate()
                    .centerCrop()
                    .override(,)
                    .priority(Priority.IMMEDIATE)
                    .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                    .skipMemoryCache(true)
                    .into(imageView);
    

    You may also need to add,

    In manifest, inside

    
            
        
    

    Inside res/xml, as file_paths.xml,

    
        
    
    

    0 讨论(0)
提交回复
热议问题