简体   繁体   English

使用Picasso无法通过ImageView中的URL加载图像

[英]Image not loading through URL in ImageView using Picasso

I am trying to set an Image in ImageView from URL using Picasso library. 我正在尝试使用Picasso库从URL在ImageView设置图像。 The image is loading from some links like this: 图片是从这样的一些链接加载的:

https://pbs.twimg.com/profile_images/638751551457103872/KN-NzuRl.png

But not loading from link like this: 但是不能从这样的链接加载:

http://imagebin.ca/v/2J37dL9JufmN

I can't figure out what the issue is. 我不知道是什么问题。 I want to load my image from second url, but it's not working. 我想从第二个URL加载图像,但是它不起作用。

Here's *MainActivity.java**: 这是* MainActivity.java **:

public class MainActivity extends AppCompatActivity {
    ImageView a;
    String Url;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        a = (ImageView) findViewById(R.id.a);

        Url = "MY_URL";

        Picasso.with(getApplicationContext())
                .load(Url)
                .placeholder(R.drawable.bday)
                .into(a);
}
}

Here's a snippet from activity_main.xml 这是来自activity_main.xml的片段

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/a" />

</LinearLayout>

I have set required permissions in manifest. 我已在清单中设置了所需的权限。 I have explored few links similar to this problems but not able to solve this issue. 我已经探索了一些与此问题类似的链接,但无法解决此问题。 Any help is appreciated. 任何帮助表示赞赏。

Try to change like: 尝试像这样进行更改:

Picasso.with(this)
            .load(Url)
            .placeholder(R.drawable.bday)
            .into(a);

Also don't forget to give 也别忘了给

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

to your project's manifest 根据您的项目清单

I myself figured out the problem. 我自己解决了问题。 I was using a indirect link like this one: http://imagebin.ca/v/2J37dL9JufmN to website hosting the image so it was not being loaded in the ImageView. 我使用的是这样的间接链接: http : //imagebin.ca/v/2J37dL9JufmN到托管图像的网站,因此未将其加载到ImageView中。 We should use the direct link to the image to load that particular image which is like this one: https://pbs.twimg.com/profile_images/638751551457103872/KN-NzuRl.png which have file name and its extension in the end. 我们应该使用指向图像的直接链接来加载该特定图像,例如: https : //pbs.twimg.com/profile_images/638751551457103872/KN-NzuRl.png ,其文件名及其扩展名最后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM