Android preloading images before it gets used
Use Picasso fetch method to fetch the image by url before the image is being used. This fetch can be run during the initialization of the app or anywhere before the image is being used.
Picasso.with(getApplicationContext()).load(url).fetch();
Use Picasso to load image from the cache by specifiying the OFFLINE policy for in newworkPolicy. If it failed to load it from offline mode, try again from the network.
Picasso .with(getApplicationContext()) .load(url) .networkPolicy(NetworkPolicy.OFFLINE) .into(imageView, new Callback() { @Override public void onSuccess() { Log.d("Picasso", "Image loaded from cache>>>" + url); } @Override public void onError() { Log.d("Picasso", "Try again in ONLINE mode if load from cache is failed"); Picasso.with(getApplicationContext()).load(url).into(imageView, new Callback() { @Override public void onSuccess() { Log.d("Picasso", "Image loaded from web>>>" + url); } @Override public void onError() { Log.d("Picasso", "Failed to load image online and offline, make sure you enabled INTERNET permission for your app and the url is correct>>>>>>>" + url); } }); } });
A complete sample app for using Picass to warm up the image first, aka pre loading the images before it gets used.
Make sure you enabled the INTERNET permission in your AndroidManifest file.
Include the Picasso library as one of the dependepcies in your app build gradle file.
compile 'com.squareup.picasso:picasso:2.5.2'
The activity_main.xml
The MainActivity.java
public class MainActivity extends AppCompatActivity { private LinearLayout llImageContainer; private String [] imgUrls= { "http://developer.android.com/images/activity_lifecycle.png", "http://developer.android.com/images/fundamentals/fragments.png", "http://developer.android.com/images/activity_fragment_lifecycle.png", "http://developer.android.com/images/fragment_lifecycle.png", }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); llImageContainer = (LinearLayout) findViewById(R.id.ll_image_container); preloadImages(); } private void preloadImages() { for(String url: imgUrls) { Picasso.with(getApplicationContext()).load(url).fetch(); } } public void loadImages(View v) { for(final String url: imgUrls) { final ImageView imageView = new ImageView(this); imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); llImageContainer.addView(imageView); Picasso.with(getApplicationContext()).load(url).networkPolicy(NetworkPolicy.OFFLINE).into(imageView, new Callback() { @Override public void onSuccess() { Log.d("Picasso", "Image loaded from cache>>>" + url); } @Override public void onError() { Log.d("Picasso", "Try again in ONLINE mode if load from cache is failed"); Picasso.with(getApplicationContext()).load(url).into(imageView, new Callback() { @Override public void onSuccess() { Log.d("Picasso", "Image loaded from web>>>" + url); } @Override public void onError() { Log.d("Picasso", "Failed to load image online and offline, make sure you enabled INTERNET permission for your app and the url is correct>>>>>>>" + url); } }); } }); } } private void loadWithLogging() { Picasso picasso = new Picasso.Builder(getApplicationContext()) .listener(new Picasso.Listener() { @Override public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) { exception.printStackTrace(); } }).build(); picasso.load("http://developer.android.com/images/activity_lifecycle.png").into(new ImageView(this)); } }
Search within Codexpedia
Custom Search
Search the entire web
Custom Search
Related Posts