1

I want to set image in to imageview,my image is coming from https url.Here i am using Picaso for loadiing image,But i am not able to get image from https url.If i change https to http then i am able to get the image,But i want to get image from https url .Please solve my problem

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_picaso_image);
    ImageView img=(ImageView)findViewById(R.id.imageView1);

    String url="https://www.bahrainlocator.gov.bh/blm_data/point_1418646022.jpg";


    Picasso.with(this).load(url).into(img);

}
S.M_Emamian
  • 17,005
  • 37
  • 135
  • 254
DurgaPrasad Ganti
  • 1,008
  • 4
  • 20
  • 41

4 Answers4

3

EDIT: Please try to use the Activity.this as the context,if you are using fragments then use getActivity()

Download picasso library from http://square.github.io/picasso/

Then try using this method

Picasso.with(context)
    .load(url)
    .placeholder(R.drawable.user_placeholder)
    .error(R.drawable.user_placeholder_error)
    .into(imageView);

And they have some issues with the high resolution images and https protocols if you still cant load with picasso then try Glide library

 String url = myUrls.get(position);

    Glide.with(myFragment)
        .load(url)
        .centerCrop()
        .placeholder(R.drawable.loading_spinner)
        .crossFade()
        .into(myImageView);

You can download Glide from here https://github.com/bumptech/glide/releases

UPDATE: For some reason the https appended links are not loaded in both glide and picasso in this case i used the Universal image loading library..

 ImageLoader imageLoader;
    ImageView iv;
    private ImageLoadingListener animateFirstListener = null;
    DisplayImageOptions doption_two = null;

Inside the onCreate

imageLoader = ImageLoader.getInstance();
        doption_two = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_launcher)
                .showImageForEmptyUri(R.drawable.icon)
                .showImageOnFail(R.drawable.icon).cacheInMemory(true)
                .cacheOnDisc(true).considerExifParams(true).build();
        animateFirstListener = new AnimateFirstDisplayListener();
        iv = (ImageView) findViewById(R.id.imageView1);

        String url = "http://www.bahrainlocator.gov.bh/blm_data/point_1418646022.jpg";

        imageLoader.displayImage(url, iv, doption_two, animateFirstListener);

Also declare a static inner class

private static class AnimateFirstDisplayListener extends
            SimpleImageLoadingListener {

        static final List<String> displayedImages = Collections
                .synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view,
                Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }

As helper classes, I have added the

UILApplication.java

public class UILApplication extends Application {
    @TargetApi(Build.VERSION_CODES.GINGERBREAD)
    @SuppressWarnings("unused")
    @Override
    public void onCreate() {
        if (Config.DEVELOPER_MODE
                && Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
            StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
                    .detectAll().penaltyDialog().build());
            StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
                    .detectAll().penaltyDeath().build());
        }

        super.onCreate();

        initImageLoader(getApplicationContext());
    }

    public static void initImageLoader(Context context) {
        // This configuration tuning is custom. You can tune every option, you
        // may tune some of them,
        // or you can create default configuration by
        // ImageLoaderConfiguration.createDefault(this);
        // method.
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                context).threadPriority(Thread.NORM_PRIORITY - 2)
                .denyCacheImageMultipleSizesInMemory()
                .discCacheFileNameGenerator(new Md5FileNameGenerator())
                .tasksProcessingOrder(QueueProcessingType.LIFO)
                .writeDebugLogs() // Remove for release app
                .build();
        // Initialize ImageLoader with configuration.
        ImageLoader.getInstance().init(config);
    }
}

And a constants.java

/**
 * @author George Thomas
 */
public final class Constants {



    public static class Config {
        public static final boolean DEVELOPER_MODE = false;
    }


}

Make sure that you make the application name in the manifest as .UILApplication

<application
        android:name=".UILApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

You can find the jar file at Univerasl Image loader

George Thomas
  • 4,566
  • 5
  • 30
  • 65
2

For load image from https url you can see my answer here

Click to see answer

Where you need to call a method given below before use Universal Image Loader to load image

 private void trustEveryone() {
 try {
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier(){
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }});
    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, new X509TrustManager[]{new X509TrustManager(){
        public void checkClientTrusted(X509Certificate[] chain,
                                       String authType) throws CertificateException {}
        public void checkServerTrusted(X509Certificate[] chain,
                                       String authType) throws CertificateException {}
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }}}, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(
            context.getSocketFactory());
    } catch (Exception e) { // should never happen
    e.printStackTrace();
    }
}
1

Try This Way...

This is the perfect way to USE PICASSO.. and you are doing it fine..

Picasso.with(_context).load(imgurl)
    .placeholder(R.drawable.placeholderImg)
    .error(R.drawable.errorImg).fit().centerInside()
    .into(imageview);

But the Problem with https there are some articles already available for it..

Take a Look on this..

Links

https://github.com/square/picasso/issues/500

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

Community
  • 1
  • 1
Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
0

use this.The OkHttpDownloader() does the trick..OkHttp3Downloader won't work.

val builder = Picasso.Builder(context)
        builder.downloader(OkHttpDownloader(context))
        val picasso = builder.build()

        var url =  imageUrl
        if (!(imageUrl?.startsWith("http", true)!!)) {
            url = context.getString(R.string.iconsBaseUrl) + imageUrl
        }
        picasso.load(url)
                .placeholder(ContextCompat.getDrawable(context, R.drawable.img_placeholder))
                .into(view.galleryImage)