简体   繁体   中英

Picasso won't load image (Android)

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!

Try Using android:usesCleartextTraffic="true" in Application Tag of your Manifest file! As i faced same issue using Android Volley!

尝试一下

   Picasso.with(getActivity()).load("http://i.imgur.com/DvpvklR.png").into(image);

I think you dont need to parse Uri. Try without 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;
}

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);

It's working for me with the following code:

Picasso.with(this)
                .load("http://i.imgur.com/DvpvklR.png")
                .into(myImageView);

I used Picasso more than once. FYI: (pic_link is a JsonElement) Hope this can helps you This is how I build my method:

 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);
        }
    });
}

Use link below

Picasso.Get().Load(imageUri).Placeholder(Resource.Drawable.patient_profile_pic).Resize(100, 100).Into(imgProflle);

without plugin

Xamarin or .Net

 byte[] imageBytes;


    using (var webClient = new WebClient())
    {
      imageBytes = webClient.DownloadData(imageUri);
    }

    Bitmap bitmap = BitmapFactory.DecodeByteArray(imageBytes, 0, imageBytes.Length);

    imgProflle.SetImageBitmap(bitmap);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM