简体   繁体   English

无法使用 Picasso (Android) 加载图像

[英]Can't load image with Picasso (Android)

I'm trying to display an image in an ImageView with the Picasso Library but I don't know why the image doesn't load.我正在尝试使用 Picasso 库在 ImageView 中显示图像,但我不知道为什么无法加载图像。 Can you help me please ?你能帮我吗 ?

In the build.gradle (dependencies) :在 build.gradle(依赖项)中:

implementation 'com.squareup.picasso:picasso:2.71828'

In the MainActivity.java :在 MainActivity.java 中:

package com.example.imageview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

public class MainActivity extends AppCompatActivity {

    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.imageView);
        Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(imageView);
    }
}

In the AndroidManifest.xml : (I don't know if it's really necessary)在AndroidManifest.xml中:(不知道是不是真的有必要)

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

In the activity_main.xml :在 activity_main.xml 中:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="154dp">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:src="#A0A" />

</RelativeLayout>
</android.support.constraint.ConstraintLayout>

I would like just display the image with the url like in the Picasso's documentation.我只想像毕加索的文档中那样显示带有 url 的图像。 Did i forget something ?我忘记了什么吗? Or change something in settings ?或者在设置中更改某些内容?

Thank you !谢谢 !

Replace below.下面替换。 Pass the Activity reference into picasso.将 Activity 引用传递给 picasso。

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

Your url is not working,您的网址无效,

Try this;尝试这个;

Picasso.get().load(" https://www.gstatic.com/webp/gallery/4.sm.jpg ").into(imageView); Picasso.get().load(" https://www.gstatic.com/webp/gallery/4.sm.jpg ").into(imageView);

instead of代替

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

Remove the去除那个

android:src="#A0A" android:src="#A0A"

from your activity_main.xml.来自您的activity_main.xml。 You don't need to put any source in the xml, you can do that with您不需要在 xml 中放置任何源,您可以使用

.load.....
.placeholder(R.drawable.user_placeholder) // Image to show before loading url of image
.error(R.drawable.user_placeholder_error) // Image to show is error occurred while loading url

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

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