0

私はAndroidアプリを開発していますが、現在は静的です。動的にするために、picasso ライブラリを使用して画像をフェッチすることを考えています。しかし、それはURLの画像を表示していません。これが私のコードです -

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView imageView = (ImageView) findViewById(R.id.imageView);

    Picasso.with(this)
            .load("http://s12.postimg.org/k07c5mzil/images_1.jpg")
            .placeholder(R.mipmap.ic_launcher)
            .error(R.mipmap.ic_launcher)
            .into(imageView);
}
}

activity_main.xml -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">


<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_gravity="center_horizontal" />
</LinearLayout>

私はandroid.manifestでインターネット許可を与えました。

また、 picasso ライブラリの依存関係 -

compile 'com.squareup.picasso:picasso:2.5.2'

しかし、まだURLの画像を見ることができません。私を助けてください...

4

2 に答える 2

0

コードを変更して、次のように変更します。テストしたところ、完全に正常に動作しています。

ImageView imageView = (ImageView) findViewById(R.id.imageViewOfurl);
    Context context = imageView.getContext();
    String url = "http://yaitisme.com/images/getImage.jpg";



    Picasso.with(context)
            .load(url)
            .placeholder(R.mipmap.ic_launcher)
            .error(R.mipmap.ic_launcher)
            .resize(50,50)
            .into(imageView);

プログラムに応じて名前を置き換えることができます。

問題の原因はサイズにある可能性があるため、サイズを変更できます。また、イメージビューの幅と高さの両方でコンテンツをラップします。インターネットの許可も。それが役に立てば幸い!

于 2015-10-24T18:55:32.773 に答える