本文介绍了如何从网址ImageView的设定图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用URL,比如我有这个网址中的ImageView设置图片

I wanna set Image in ImageView using Url for example I have this url

<一个href="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" rel="nofollow">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

但没有选项来设置网址

推荐答案

编辑:

通过使用AsychTask

With using AsychTask

木箱类像

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

}

和调用这个像新ImageLoadTask(URL,ImageView的).execute();

And call this like new ImageLoadTask(url, imageView).execute();

直接法:

使用这种方法,并通过你的网址字符串,它retuns你的位图,然后将其设置为ImageView的..

Use this method and pass your url as string it retuns you a bitmap then set it to 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如..

And then this to ImageView like..

imageView.setImageBitmap(getBitmapFromURL(url));

和不要忘记在maifest此权限。

And dont forget about this permission in maifest.

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

注意:

Try to call this method from another thread or AsynchTask because we are performing networking operations.

这篇关于如何从网址ImageView的设定图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 09:49