Doesn't Picasso support to download images which uses https protocol

前端 未结 5 1789
死守一世寂寞
死守一世寂寞 2020-12-01 12:38

Hello I am using the Picasso library to download the images from URL.

URL : https://fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/t1.0-1/s200x200/14728

5条回答
  •  温柔的废话
    2020-12-01 13:42

    @nexus700120 solution is not up to date

    @Neha Tyagi solution works but creates multiple instances of Picasso in each instance of activity which leaks memory

    So I crafted a up to date and singleton solution for redirecting image URLs -

    CustomPicasso.java

    import android.content.Context;
    import android.util.Log;
    
    import com.jakewharton.picasso.OkHttp3Downloader;
    import com.squareup.picasso.Picasso;
    
    /**
     * Created by Hrishikesh Kadam on 19/12/2017
     */
    
    public class CustomPicasso {
    
        private static String LOG_TAG = CustomPicasso.class.getSimpleName();
        private static boolean hasCustomPicassoSingletonInstanceSet;
    
        public static Picasso with(Context context) {
    
            if (hasCustomPicassoSingletonInstanceSet)
                return Picasso.with(context);
    
            try {
                Picasso.setSingletonInstance(null);
            } catch (IllegalStateException e) {
                Log.w(LOG_TAG, "-> Default singleton instance already present" +
                        " so CustomPicasso singleton cannot be set. Use CustomPicasso.getNewInstance() now.");
                return Picasso.with(context);
            }
    
            Picasso picasso = new Picasso.Builder(context).
                    downloader(new OkHttp3Downloader(context))
                    .build();
    
            Picasso.setSingletonInstance(picasso);
            Log.w(LOG_TAG, "-> CustomPicasso singleton set to Picasso singleton." +
                    " In case if you need Picasso singleton in future then use Picasso.Builder()");
            hasCustomPicassoSingletonInstanceSet = true;
    
            return picasso;
        }
    
        public static Picasso getNewInstance(Context context) {
    
            Log.w(LOG_TAG, "-> Do not forget to call customPicasso.shutdown()" +
                    " to avoid memory leak");
    
            return new Picasso.Builder(context).
                    downloader(new OkHttp3Downloader(context))
                    .build();
        }
    }
    

    build.gradle (Module:app)

    android {
    
        ...
    
    }
    
    dependencies {
    
        ...
    
        compile 'com.squareup.picasso:picasso:2.5.2'
        compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'
    }
    

    Usage -

    CustomPicasso.with(context)
        .load("http://i.imgur.com/DvpvklR.png")
        .into(imageView);
    

    For latest revisions check CustomPicasso gist - https://gist.github.com/hrishikesh-kadam/09cef31c736de088313f1a102f5ed3a3

提交回复
热议问题