首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从url到可绘制或位图的图像:最好、最快的方法

从url到可绘制或位图的图像:最好、最快的方法
EN

Stack Overflow用户
提问于 2011-08-08 19:55:36
回答 3查看 6.8K关注 0票数 0

我尝试在我的应用程序中显示来自url的图片。但是我使用的方法很长。这是我在stackoverflow上创建的代码

代码语言:javascript
运行
复制
public  Bitmap getImage(String url,String src_name) throws java.net.MalformedURLException, java.io.IOException {
            Bitmap bitmap;
            HttpURLConnection connection = (HttpURLConnection)new URL(url) .openConnection();
            connection.setRequestProperty("User-agent","Mozilla/4.0");

            connection.connect();
            InputStream input= connection.getInputStream();

            bitmap = BitmapFactory.decodeStream(input);

            return bitmap;
}

10张图片在10-12秒内加载。如果使用此代码。

代码语言:javascript
运行
复制
 ///==========================================================================================================================================
     public   Drawable getImage(String url, String src_name) throws java.net.MalformedURLException, java.io.IOException 
        {

         Drawable abc =Drawable.createFromStream(((java.io.InputStream)new java.net.URL(url).getContent()), src_name);

            return abc;


        }   

如果使用此代码,则在9-11秒内加载图像。图像不大。最大宽度或高度为400-450。当然,我在循环中告诉这个函数是这样的:for (int i =0;i<10;i++){image[i]=getImage(url);}可以告诉我如何在我的应用程序中最佳和快速显示图像吗?打招呼,彼得。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-08-08 22:53:40

你不能放弃下载和解码图像所需的时间。数字'10‘只是图像质量的一个函数,你只能尝试在这个数字上进行优化。

如果服务器是由您管理的,那么您可能需要花费一些时间来根据您的UI需求优化可下载图像的大小。也可以尝试延迟加载(我希望您不是在UI线程上执行这些操作)。许多延迟下载和延迟解码的解决方案已经被讨论了很多次:http://www.google.com.sg/search?q=android+images+lazy+load&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a

Sidenote:不鼓励使用HttpURLConnection。使用HttpClient。这也可能会影响性能。看一看http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/

票数 1
EN

Stack Overflow用户

发布于 2012-05-10 23:36:10

代码语言:javascript
运行
复制
public static Bitmap getBitmapFromUrl(String url) {
    Bitmap bitmap = null;
    HttpGet httpRequest = null;
    httpRequest = new HttpGet(url);
    HttpClient httpclient = new DefaultHttpClient();

    HttpResponse response = null;
    try {
        response = (HttpResponse) httpclient.execute(httpRequest);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (response != null) {
        HttpEntity entity = response.getEntity();
        BufferedHttpEntity bufHttpEntity = null;
        try {
            bufHttpEntity = new BufferedHttpEntity(entity);
        } catch (IOException e) {
            e.printStackTrace();
        }

        InputStream instream = null;
        try {
            instream = bufHttpEntity.getContent();
        } catch (IOException e) {
            e.printStackTrace();
        }

        bitmap = BitmapFactory.decodeStream(instream);
    }
    return bitmap;
}
票数 0
EN

Stack Overflow用户

发布于 2012-05-10 23:41:46

代码语言:javascript
运行
复制
public static Bitmap decodeFile(String filePath) {
    // Decode image size
    BitmapFactory.Options o = new BitmapFactory.Options();
    o.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, o);

    // The new size we want to scale to
    final int REQUIRED_SIZE = 1024;

    // Find the correct scale value. It should be the power of 2.
    int width_tmp = o.outWidth, height_tmp = o.outHeight;
    int scale = 1;
    while (true) {
        if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
            break;
        width_tmp /= 2;
        height_tmp /= 2;
        scale *= 2;
    }

    // Decode with inSampleSize
    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;
    Bitmap bitmap = BitmapFactory.decodeFile(filePath, o2);
    return bitmap;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6981705

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档