【问题标题】:How to set image to imageview from https url android?如何从 https url android 将图像设置为 imageview?
【发布时间】:2018-05-10 22:04:42
【问题描述】:

我想将图像设置为 imageview,我的图像来自 https url。这里我使用 Picaso 加载图像,但我无法从 https url 获取图像。如果我将 https 更改为 http,那么我就是能够获取图像,但我想从 https url 获取图像。请解决我的问题

 @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);

}

【问题讨论】:

  • 你遇到了什么问题?
  • 可能是网络不工作或清单中未添加权限
  • 没有网络,我也添加了权限。如果将 https 更改为 http,则图像即将到来

标签: android


【解决方案1】:

编辑: 请尝试使用 Activity.this 作为上下文,如果您使用的是片段,则使用 getActivity()

http://square.github.io/picasso/下载毕加索库

那就试试这个方法

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

如果您仍然无法加载毕加索,那么他们在高分辨率图像和 https 协议方面存在一些问题,然后尝试 滑翔库

 String url = myUrls.get(position);

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

您可以从这里下载 Glide https://github.com/bumptech/glide/releases

更新: 由于某种原因,glide 和 picasso 中都没有加载 https 附加链接,在这种情况下,我使用了通用图像加载库..

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

在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);

同时声明一个静态内部类

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);
                }
            }
        }
    }

作为辅助类,我添加了

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);
    }
}

还有一个constants.java

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



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


}

确保将清单中的应用程序名称设为 .UILApplication

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

你可以在Univerasl Image loader找到jar文件

【讨论】:

  • @Arash:是的,这就是我提供另一个库来加载图像的原因。因为我之前已经回答过,所以我只是复制了答案,请兄弟们多多包涵。
  • 嗨@George Thomas,你找到任何解决方案了吗
  • 嗨,durga,我也为我尝试了这两个都是同样的问题,最后我尝试了通用图像加载器,它就像一个魅力..
  • 如果您仍有疑问,我可以将示例应用程序发送给您!通过我的电子邮件联系我[您可以在我的 stackoverflow 个人资料中找到它]
  • 能否请您发送示例应用程序
【解决方案2】:

对于从 https url 加载图片,您可以在此处查看我的答案

Click to see answer

在使用通用图像加载器加载图像之前,您需要调用下面给出的方法

 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();
    }
}

【讨论】:

    【解决方案3】:

    试试这个方法...

    这是使用 PICASSO 的完美方式.. 你做得很好..

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

    但是 https 的问题已经有一些文章可用..

    看看这个..

    链接

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

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

    【讨论】:

      【解决方案4】:

      使用这个。OkHttpDownloader() 可以解决问题。OkHttp3Downloader 不起作用。

      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)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-13
        • 2014-03-27
        • 2016-02-03
        • 2013-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多