Android - Picasso从网址加载

时间:2018-02-22 19:48:53

标签: android imageview picasso

我想尝试毕加索图书馆。但我无法在简单的代码下运行。

我用gradle添加毕加索: compile 'com.android.support:appcompat-v7:26+' compile 'com.squareup.picasso:picasso:2.5.2'

我为展示添加了互联网权限。 .... <uses-permission android:name="android.permission.INTERNET"/> <application....

示例代码:

imgView = (ImageView) findViewById(R.id.imgView);
imgView.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        String url = "http://i.imgur.com/DvpvklR.png";
        Picasso.with(myActivity).load(url).into(imgView);
        return true;
    }
});

xml文件中的imageview:

<ImageView
    android:layout_width="300dp"
    android:layout_height="400dp"
    android:id="@+id/imgView"
    android:background="@color/colorPrimary"
    />

从drawable文件夹加载。但是我无法从网址上看到任何图片加载。

修改

imgView.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.i("tag", "onClick");
            String url = "http://i.imgur.com/DvpvklR.png";
            Picasso.with(myActivity).setLoggingEnabled(true);
            Picasso.with(myActivity).load(url).into(imgView, new Callback() {
                @Override
                public void onSuccess() {
                    Log.e("tag", "onSuccess");
                }

                @Override
                public void onError() {
                    Log.e("tag", "OnError");
                }
            });
        }
    });

OnClick不起作用。 Logcat在下面:

02-22 23:08:32.283 21749-21749/? D/Picasso: Main        created      [R2] Request{http://i.imgur.com/DvpvklR.png}
02-22 23:08:32.283 21749-21777/? D/Picasso: Dispatcher  enqueued     [R2]+0ms 
02-22 23:08:32.283 21749-21779/? D/Picasso: Hunter      executing    [R2]+2ms 
02-22 23:08:32.793 21749-21777/? D/Picasso: Dispatcher  retrying     [R2]+509ms 
02-22 23:08:32.793 21749-21781/? D/Picasso: Hunter      executing    [R2]+510ms 
02-22 23:08:33.303 21749-21777/? D/Picasso: Dispatcher  retrying     [R2]+1015ms 
02-22 23:08:33.303 21749-21782/? D/Picasso: Hunter      executing    [R2]+1018ms 
02-22 23:08:33.313 21749-21777/? D/Picasso: Dispatcher  batched      [R2]+1028ms for error
02-22 23:08:33.523 21749-21749/? D/Picasso: Main        errored      [R2]+1232ms 
02-22 23:08:33.523 21749-21777/? D/Picasso: Dispatcher  delivered    [R2]+1231ms 

EDIT2

有人说尝试这个链接。 http://thumbs2.ebaystatic.com/m/mxQFkSjRujUpASIMht_snyQ/140.jpg

它对我的三星设备不起作用。它与响应时间有关吗?

2 个答案:

答案 0 :(得分:0)

尝试添加onClick侦听器而不是onTouch侦听器:

'648x648'

答案 1 :(得分:0)

对于错误记录启用:

Picasso.with(Context).setLoggingEnabled(true);

在您的代码中:

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

另一个建议:在布局xml中:添加android:adjustViewBounds="true"

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imgView"
android:adjustViewBounds="true"
android:background="@color/colorPrimary"
/>