27

たとえば、URLを使用してImageViewに画像を設定したいのですが、このURLがあります

http://www.google.iq/imgres?hl=en&biw=1366&bih=667&tbm=isch&tbnid=HjzjsaANDXVR9M:&imgrefurl=http://www.vectortemplates.com/raster-batman.php&docid=FxbVmggVf--0dM&imgurl=http:/ /www.vectortemplates.com/raster/batman-logo-big.gif&w=2072&h=1225&ei=Zeo_UoSWIMaR0AXl_YHIBg&zoom=1

ただし、URLを設定するオプションはありません

4

11 に答える 11

65

編集:

AsyncTaskを拡張するクラスを作成する

public class ImageLoadTask extends AsyncTask<Void, Void, Bitmap> {

    private String url;
    private ImageView imageView;

    public ImageLoadTask(String url, ImageView imageView) {
        this.url = url;
        this.imageView = imageView;
    }

    @Override
    protected Bitmap doInBackground(Void... params) {
        try {
            URL urlConnection = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) urlConnection
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            Bitmap myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);
        imageView.setImageBitmap(result);
    }

}

そして、これを次のように呼び出しますnew ImageLoadTask(url, imageView).execute();

直接法:

このメソッドを使用して、URL を文字列として渡します。ビットマップを返します。ビットマップを ImageView に設定します。

public static Bitmap getBitmapFromURL(String src) {
    try {
        Log.e("src",src);
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        Log.e("Bitmap","returned");
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        Log.e("Exception",e.getMessage());
        return null;
    }
}

そして、これを ImageView に次のように:

imageView.setImageBitmap(getBitmapFromURL(url));

そして、maifest でのこの許可を忘れないでください。

<uses-permission android:name="android.permission.INTERNET" />

ノート:

ネットワーク操作を実行しているため、別のスレッドまたは AsyncTask からこのメソッドを呼び出すようにしてください。

于 2013-09-23T07:19:12.413 に答える
45

Square のPicassoライブラリに面倒な作業を任せることもできます。

Picasso
    .get()
    .load("http://...")
    .into(imageView);

おまけとして、キャッシング、変換などを利用できます。

于 2015-07-22T12:15:30.277 に答える
9

試す:

URL newurl = new URL(photo_url_str); 
mIcon_val = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream()); 
profile_photo.setImageBitmap(mIcon_val);

もっと見る

1) url-in-android による画像ビューのロード方法

2) android-make-an-image-at-a-url-equal-to-imageviews-image

于 2013-09-23T07:18:44.633 に答える
1

Aqueryライブラリを使用する簡単な方法URL から直接読み込み画像を取得するのに役立ちます

AQuery aq=new AQuery(this); // intsialze aquery
 aq.id(R.id.ImageView).image("http://www.vikispot.com/z/images/vikispot/android-w.png");
于 2015-11-04T13:14:52.720 に答える
-2

これを試して :

ImageView imageview = (ImageView)findViewById(R.id.your_imageview_id);
Bitmap bmp = BitmapFactory.decodeFile(new java.net.URL(your_url).openStream());  
imageview.setImageBitmap(bmp);

これを試すこともできます: Android-Universal-Image-Loaderから画像を効率的に読み込むURL

于 2013-09-23T07:19:12.900 に答える