I'm trying to load image to ImageView using Picasso. The most basic stuff.
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.
Here is my code.
In MainActivity.java, I have
@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
@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>
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)
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: Logcat I 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..
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.
Thanks again for all suggestion!
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://image.tmdb.org/t/p/w185//2uNW4WbgBXL25BAbXGLnLqX71Sw.jpg
The main error is in your manifest file.
You should correct :
<user-permission android:name="android.permission.INTERNET" />
To:
<uses-permission android:name="android.permission.INTERNET" />
And all the pictures will work!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With