我正在尝试使用Picasso Library在ImageView中显示图像,但是我不知道为什么不加载该图像。你能帮我吗?
在build.gradle中(依赖项):
implementation 'com.squareup.picasso:picasso:2.71828'
在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);
}
}
在AndroidManifest.xml中: (我不知道这是否真的必要)
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
在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>
我只想显示带有Picasso文档中的url的图像。我忘记了什么吗?或更改设置?
谢谢!
答案 0 :(得分:0)
在下面替换。将活动参考传递到毕加索。
Picasso.with(MainActivity.this)
.load("http://i.imgur.com/DvpvklR.png").into(imageView);
答案 1 :(得分:0)
您的网址无效
尝试一下;
Picasso.get()。load(“ https://www.gstatic.com/webp/gallery/4.sm.jpg”)。into(imageView);
代替
Picasso.get()。load(“ http://i.imgur.com/DvpvklR.png”)。into(imageView);
答案 2 :(得分:0)
删除
android:src =“#A0A”
来自您的activity_main.xml。您不需要在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