java Picasso 不会加载图像 (Android)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35936767/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Picasso won't load image (Android)
提问by Mind
I'm trying to load image to ImageView using Picasso. The most basic stuff.
我正在尝试使用 Picasso 将图像加载到 ImageView。最基本的东西。
I know it's straightforward and I'm pretty sure I did it properly but it wouldn't work for some reason. No photo is shown in my app. I have found someone having the same issue (this, for example, Android - Picasso in Android Studio doesn't load) and my code is almost exactly the same as the fixed version of theirs, but it still wouldn't work.
我知道这很简单,而且我很确定我做得对,但由于某种原因它不起作用。我的应用程序中没有显示照片。我发现有人遇到了同样的问题(例如,Android - Android Studio 中的 Picasso 无法加载)并且我的代码几乎与他们的固定版本完全相同,但它仍然无法正常工作。
Here is my code.
这是我的代码。
In MainActivity.java, I have
在 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();
}
And in MyFragment.java, I have
在 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
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>
Manifest file
清单文件
<?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 (module:app)
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();
}
Any suggestion would be appreciated!
任何建议将不胜感激!
EDIT: LogcatI got some messages in Logcat from Picasso previously, but I checked again and now there is no log from Picasso. It looked exactly like in the other thread that has the same issue which looks like this..
编辑: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
UPDATE:
I figured it out! It was because target sdk version in my app was 23, while my phone is using KitKat. When I changed dependency in gradle to compile 'com.android.support:appcompat-v7:21.0.2'
instead of compile 'com.android.support:appcompat-v7:23.1.1'
, it works.
更新:
我想通了!这是因为我的应用程序中的目标 sdk 版本是 23,而我的手机使用的是 KitKat。当我将 gradle 中的依赖项改为 compile 'com.android.support:appcompat-v7:21.0.2'
而不是 时compile 'com.android.support:appcompat-v7:23.1.1'
,它起作用了。
Thanks again for all suggestion!
再次感谢所有建议!
回答by Lapenkov Vladimir
The main error is in your manifest file.
主要错误在您的清单文件中。
You should correct :
你应该更正:
<user-permission android:name="android.permission.INTERNET" />
<user-permission android:name="android.permission.INTERNET" />
To:
到:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.INTERNET" />
And all the pictures will work!
所有的图片都会起作用!
回答by dave o grady
I noticed that when I was using an emulator I could not see the image and solved it using a simple fix.
我注意到当我使用模拟器时我看不到图像并使用简单的修复解决了它。
Make sure you use https instead of http
确保使用 https 而不是 http
https://image.tmdb.org/t/p/w185//2uNW4WbgBXL25BAbXGLnLqX71Sw.jpg
回答by erfanhy
Try Using android:usesCleartextTraffic="true"
in Application Tag of your Manifest file! As i faced same issue using Android Volley!
尝试android:usesCleartextTraffic="true"
在您的清单文件的应用程序标签中使用!因为我在使用 Android Volley 时遇到了同样的问题!
回答by thuongle
try it
试试看
Picasso.with(getActivity()).load("http://i.imgur.com/DvpvklR.png").into(image);
回答by Kristiyan Varbanov
I think you dont need to parse Uri. Try without Uri.parse
我认为您不需要解析 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;
}
回答by MrZ
this work for me :
这对我有用:
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);
回答by Isaac Urbina
It's working for me with the following code:
它使用以下代码对我来说有效:
Picasso.with(this)
.load("http://i.imgur.com/DvpvklR.png")
.into(myImageView);
回答by Alexiscanny
I used Picasso more than once. FYI: (pic_link is a JsonElement) Hope this can helps you This is how I build my method:
我不止一次使用毕加索。仅供参考:(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);
}
});
}