无法在 Android 中使用 Picasso 从 URL 加载图像

Can't load image from URL with Picasso in Android

我一直在为我的一个项目尝试使用 Picasso 库,但它似乎不起作用,而且我找不到解决方案。

我的代码看起来不错,我创建了一个新项目来测试库。这是测试项目中的代码:

XML 文件:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

    <ImageView
        android:id="@+id/imagine"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainAcitivity.java

package com.example.test;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

public class MainActivity extends AppCompatActivity {

private ImageView imagine;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    imagine = findViewById(R.id.imagine);
    Picasso.get().load("http://i.imgur.com/DvpvklR.png").into(imagine);

}
}

清单文件:

<?xml version="1.0" encoding="utf-8"?>

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:usesCleartextTraffic="true">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

即使是这个简单的项目,图像也不会加载,我似乎找不到问题所在。

尝试使用以下代码加载图像,

 Picasso.get().load("http://i.imgur.com/DvpvklR.png")
                    .placeholder(R.drawable.imgplaceholder)
                    .error(R.drawable.imgplaceholder)
                    .into(ivProfilePicture)

尝试替换

get()

with()

Picasso.with(getApplicationContext()).load("http://i.imgur.com/DvpvklR.png").into(imagine);

just like here

这就是我在 Picasso 中检查 exception 的方式,如果图像由于某些 error.

imagine = findViewById(R.id.imagine);
Picasso picasso = new Picasso.Builder(parent.getContext())
            .listener(new Picasso.Listener() {
                @Override
                public void onImageLoadFailed(Picasso picasso, Uri uri, Exception exception) {
                    // check here the exception
                }
            })
            .build();

picasso.load("http://i.imgur.com/DvpvklR.png")
            .into(imagine);