什么是得到URL的图像的ImageView Android中的最佳途径 [英] What is the best way to get an image from URL to ImageView in Android

查看:118
本文介绍了什么是得到URL的图像的ImageView Android中的最佳途径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的方法从URL获取图像点击一个按钮时,但它得到了太多的时间,约4-5秒钟才能显示图像大小只有6使用3G没有的Wi-Fi KB。

我的方法是这样的;

 公共静态位图getBitmapFromURL(的字符串src){
    尝试{
        Log.e(src用户,SRC);
        网址URL =新的URL(SRC);
        HttpURLConnection的连接=(HttpURLConnection类)url.openConnection();
        connection.setDoInput(真);
        connection.connect();
        输入的InputStream = connection.getInputStream();
        位图MYBITMAP = BitmapFactory.de codeStream(输入);
        Log.e(位图,返回);
        返回MYBITMAP;
    }赶上(IOException异常五){
        e.printStackTrace();
        Log.e(异常,e.getMessage());
        返回null;
    }
}

然后

 位图bimage = getBitmapFromURL(http://www.replikler.net/test2.jpg);
    ImageView的图像=(ImageView的)findViewById(R.id.movi​​e_image);
    image.setImageBitmap(bimage);

你知道另一种方式(快)从URL得到的图像?


解决方案

  imageView.setImageDrawable(Drawable.createFromStream((InputStream的)新的URL(网址).getContent(),SRC ));

I am using a method to get the Image from URL when a button is clicked but it gets too much time, about 4-5 seconds to show an image sized only 6 kb using 3G not wi-fi.

My method is this;

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

and then;

    Bitmap bimage =  getBitmapFromURL("http://www.replikler.net/test2.jpg");
    ImageView image = (ImageView)findViewById(R.id.movie_image);
    image.setImageBitmap(bimage);

do you know another way (faster) to get an image from url ?

解决方案

imageView.setImageDrawable(Drawable.createFromStream((InputStream)new URL(url).getContent(), "src"));

这篇关于什么是得到URL的图像的ImageView Android中的最佳途径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆