In MainActivity, I’m loading some images using glide into recyclerview according to imageview size.
See:
@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" />
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.
“
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.
So how is it possible?
In SpalshActivity, I’m already caching Images, but it is again downloading/resizing in MainActivity for the very first time.
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(); } }
Advertisement
Answer
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); } } }
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);
You may also need to add,
In manifest, inside <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>
Inside res/xml
, as 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>