Android 来自 URL 的 ImageView:未设置(使用 Picasso)

Android ImageView From URL: Not Getting Set (Using Picasso)

我是 Android 开发的新手,所以我可能在做一些愚蠢的事情。知道为什么这段代码不起作用吗?

它编译并运行,但实际上并没有成功设置图像(之前设置为灰色背景)。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_activity);
    Button getImageButton = (Button) (findViewById(R.id.btnGetImage));
    getImageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ImageView myImageView = (ImageView) (findViewById(R.id.myImageView));
            Picasso.with(v.getContext()).load("https://www.example.com/someimage").into(myImageView);
        }
    });
}

alter you code this way

Picasso.with(YOUR_ACTIVITY_NAME.this).load("https://www.example.com/someimage").into(myImageView);

这里v.getContext()与主上下文无关,所以帮不了你,我们需要传递当前activity的上下文

请试试这个并告诉我。

 @Override
    protected void onCreate (Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_activity);
        Button getImageButton = (Button) (findViewById(R.id.btnGetImage));
        ImageView myImageView = (ImageView) (findViewById(R.id.myImageView));
        getImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Picasso.with(YourActivityName.this).load("https://www.example.com/someimage").into(myImageView);
            }
        });
    }

叮叮这里是我的答案

我确定你添加了依赖项

compile 'com.squareup.picasso:picasso:2.5.2'

并使用了正确的 url

Picasso.with(v.getContext()).load("http://i.imgur.com/DvpvklR.png").into(imageView);


我告诉你哪里错了!!

魔法:你有没有添加上网权限?它需要在您的清单中

 <uses-permission android:name="android.permission.INTERNET" />

而不是你的代码就好了

但我建议你使用 getApplicationContext()

从我的评论中复制

Picasso is a library and not an application. While creating Picasso instance if you'll not pass context, then how do you think it will get the application context from ? For it to work it requires context , and it definitely needs to be provided by the application using this library.It also prevents leaking an Activity (if that's what you pass ,some of below answers have used ) by switching to the application context. use getApplicationContext()

随机在这里回答的人也请阅读这篇文章

  • View.getContext(): Returns 视图当前 运行 所在的上下文。通常是当前活动的 Activity.

  • Activity.getApplicationContext():Returns整个应用的context(进程所有的Activity都在运行里面 的)。如果需要,请使用它而不是当前的 Activity 上下文 上下文与整个应用程序的生命周期相关,而不仅仅是 当前 Activity.

  • ContextWrapper.getBaseContext():如果您需要从另一个上下文中访问一个上下文,您可以使用 ContextWrapper。这 从 ContextWrapper 内部引用的上下文通过 getBaseContext().

所以他访问 currant activity 上下文的任何方式。

干杯!

步骤 1 在清单中检查互联网连接

<uses-permission android:name="android.permission.INTERNET" />

步骤 2compile "com.squareup.picasso:picasso:2.4.0"

第 3 步图像 url 未在浏览器中显示任何图像 将您的 url 粘贴到浏览器中,如果您获得图像,则可以将该图像设置为 imagview

第 4 步检查 url 是否正常工作 imagfile 没有 .png 或 .jpg 扩展名

@Override

     protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my_activity);

            Button getImageButton = (Button) (findViewById(R.id.btnGetImage));

            getImageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {   
   //Initializing the ImageView
    imageView = (ImageView) findViewById(R.id.imageView);

    //Loading Image from URL
    Picasso.with(this)
            .load("https://www.example.com/someimage")
            .placeholder(R.drawable.placeholder)   // optional
            .error(R.drawable.error)      // optional
            .resize(400,400)                        // optional
            .into(imageView);
}
});
}