毕加索未从 url 加载图像
Picasso Not Loading image from url
所以我尝试使用 Picasso 将图像从 url(有效)简单地加载到 imageView。我已经通过 gradle 添加了 Picasso libary 并且我添加了两个:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
正如许多其他用户指出的那样,我的代码。
我没有任何错误,即使在 10 多秒后它也没有做任何事情。
这是我的 Java 方法:
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image=(ImageView)findViewById(R.id.image);
Picasso.with(getApplicationContext()).load("http://i.imgur.com/DvpvklR.png").into(image);
}
和我的 activity XML:
<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="com.example.benhouse.testproj.MainActivity">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="0dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/image"/>
</RelativeLayout>
关于如何让它工作的任何想法?
我确定我在某个地方犯了一个明显的错误。
如果您想使用约束布局并使图像居中,您需要对相对布局使用以下约束..
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
我建议在这里尝试两件事。
1.Provide 图片占位符以确保 Picasso 正常工作。
Picasso.with(context)
.load(imageUrl)
.placeholder(R.drawable.image_name)
2.Write 在清单中两次使用权限。 [我知道这听起来很疯狂,但 grade/studio 的某个版本有这个错误]
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
你的parent Relative Layout height and width是0dp,所以childLayout(ImageView)无法显示image。所以,你需要设置Relative layout height and width wrap_content or match_parent。像那样:
<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="com.example.benhouse.testproj.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/image"/>
</RelativeLayout>
尝试在清单文件的应用程序标签中使用 android:usesCleartextTraffic="true"
!
所以我尝试使用 Picasso 将图像从 url(有效)简单地加载到 imageView。我已经通过 gradle 添加了 Picasso libary 并且我添加了两个:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
正如许多其他用户指出的那样,我的代码。 我没有任何错误,即使在 10 多秒后它也没有做任何事情。 这是我的 Java 方法:
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image=(ImageView)findViewById(R.id.image);
Picasso.with(getApplicationContext()).load("http://i.imgur.com/DvpvklR.png").into(image);
}
和我的 activity XML:
<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="com.example.benhouse.testproj.MainActivity">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="0dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/image"/>
</RelativeLayout>
关于如何让它工作的任何想法? 我确定我在某个地方犯了一个明显的错误。
如果您想使用约束布局并使图像居中,您需要对相对布局使用以下约束..
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
我建议在这里尝试两件事。
1.Provide 图片占位符以确保 Picasso 正常工作。
Picasso.with(context)
.load(imageUrl)
.placeholder(R.drawable.image_name)
2.Write 在清单中两次使用权限。 [我知道这听起来很疯狂,但 grade/studio 的某个版本有这个错误]
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
你的parent Relative Layout height and width是0dp,所以childLayout(ImageView)无法显示image。所以,你需要设置Relative layout height and width wrap_content or match_parent。像那样:
<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="com.example.benhouse.testproj.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/image"/>
</RelativeLayout>
尝试在清单文件的应用程序标签中使用 android:usesCleartextTraffic="true"
!