[英]Picasso won't load image (Android)
我正在尝试使用 Picasso 将图像加载到 ImageView。 最基本的东西。
我知道这很简单,而且我很确定我做得对,但由于某种原因它不起作用。 我的应用程序中没有显示照片。 我发现有人遇到了同样的问题(例如, Android - Android Studio 中的 Picasso 无法加载)并且我的代码几乎与他们的固定版本完全相同,但它仍然无法正常工作。
这是我的代码。
在 MainActivity.java 中,我有
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new MyFragment())
.commit();
}
在 MyFragment.java 中,我有
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ImageView image = (ImageView) rootView.findViewById(R.id.movie_image);
Picasso.with(getActivity()).load(Uri.parse("http://i.imgur.com/DvpvklR.png")).into(image);
return rootView;
}
fragment_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/movie_image" />
</FrameLayout>
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.admin.popmovies">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<user-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>
build.gradle(模块:应用程序)
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.example.admin.popmovies"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.squareup.picasso:picasso:2.5.2'
}
repositories {
mavenCentral();
}
任何建议将不胜感激!
编辑:Logcat我之前在 Logcat 中收到了来自 Picasso 的一些消息,但我再次检查,现在没有来自 Picasso 的日志。 它看起来与在另一个线程中具有相同问题的线程完全一样,看起来像这样..
02-23 16:41:36.857 28865-28865/com.example.enrico.prova D/Picasso﹕ Main created [R1] Request{http://www.online-image-editor.com//styles/2014/images/example_image.png}
02-23 16:41:36.858 28865-28884/com.example.enrico.prova D/Picasso﹕ Dispatcher enqueued [R1]+0ms
02-23 16:41:36.876 28865-29238/com.example.enrico.prova D/Picasso﹕ Hunter executing [R1]+16ms
02-23 16:41:36.883 28865-28884/com.example.enrico.prova D/Picasso﹕ Dispatcher batched [R1]+25ms for error
02-23 16:41:37.112 28865-28884/com.example.enrico.prova D/Picasso﹕ Dispatcher delivered [R1]+254ms
02-23 16:41:37.112 28865-28865/com.example.enrico.prova D/Picasso﹕ Main errored [R1]+255ms
更新:
我想到了! 这是因为我的应用程序中的目标 sdk 版本是 23,而我的手机使用的是 KitKat。 当我更改 gradle 中的依赖项以compile 'com.android.support:appcompat-v7:21.0.2'
而不是compile 'com.android.support:appcompat-v7:23.1.1'
,它可以工作。
再次感谢所有建议!
我注意到当我使用模拟器时我看不到图像并使用简单的修复程序解决了它。
确保使用 https 而不是 http
https://image.tmdb.org/t/p/w185//2uNW4WbgBXL25BAbXGLnLqX71Sw.jpg
主要错误在您的清单文件中。
你应该更正:
<user-permission android:name="android.permission.INTERNET" />
到:
<uses-permission android:name="android.permission.INTERNET" />
所有的图片都会起作用!
尝试在清单文件的应用程序标签中使用android:usesCleartextTraffic="true"
! 因为我在使用 Android Volley 时遇到了同样的问题!
尝试一下
Picasso.with(getActivity()).load("http://i.imgur.com/DvpvklR.png").into(image);
我认为您不需要解析 Uri。 尝试不使用Uri.parse
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
ImageView image = (ImageView) rootView.findViewById(R.id.movie_image);
Picasso.with(getActivity()).load("http://i.imgur.com/DvpvklR.png").into(image);
return rootView;
}
这对我有用:
Picasso.with(getActivity())
.load("http://i.imgur.com/DvpvklR.png")
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.resizeDimen(R.dimen.list_detail_image_size,R.dimen.list_detail_image_size)
.centerInside()
.into(image);
它使用以下代码为我工作:
Picasso.with(this)
.load("http://i.imgur.com/DvpvklR.png")
.into(myImageView);
我不止一次使用毕加索。 仅供参考:(pic_link 是一个 JsonElement)希望这可以帮助你这是我构建我的方法的方式:
public void updatePic(JsonElement jsonObject){
Activity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
final ImageView imageUser=(ImageView)findViewById(R.id.user_profile_photo);
final Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
imageUser.setImageBitmap(bitmap);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
imageUser.setTag(target);
Picasso.with(getApplicationContext())
.load(pic_link.getAsString())
.into(target);
}
});
}
使用下面的链接
Picasso.Get().Load(imageUri).Placeholder(Resource.Drawable.patient_profile_pic).Resize(100, 100).Into(imgProflle);
没有插件
Xamarin 或 .Net
byte[] imageBytes;
using (var webClient = new WebClient())
{
imageBytes = webClient.DownloadData(imageUri);
}
Bitmap bitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);
imgProflle.SetImageBitmap(bitmap);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.