如何从https url android设置图像到imageview?

时间:2014-12-16 06:04:23

标签: android

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

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

}

3 个答案:

答案 0 :(得分:3)

编辑: 请尝试使用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协议方面存在一些问题 Glide库

 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

更新: 出于某种原因,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文件

答案 1 :(得分:1)

试试这个......

这是使用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

答案 2 :(得分:0)

对于来自https网址的加载图片,您可以在此处查看我的答案

Click to see answer

在使用Universal Image Loader加载图像

之前,需要调用下面给出的方法
  <% if Rails.env.development? %>
    <% debug_method = debug(params).split.each do |word|  %>
      <% if word  == 'controller:' %>
        <ul><% word.upcase %></ul>
      <% end %>
     <% end %>
    <%= debug_method.join %>
  <% end %>