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

发布于 11-28 11:00 字数 1265 浏览 1 评论 0原文

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

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-12 秒内加载了 10 张图像。如果使用此代码。

如果

 ///==========================================================================================================================================
     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);} 谁能告诉我如何在我的应用程序中最好和最快地显示图像? 问候,彼得。

im tried to show images from url in my app. But ways which im using is very long .
this code i founded on stackoverflow

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 images loaded in 10-12 second. if used this code.

and

 ///==========================================================================================================================================
     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;


        }   

if using this code - images loaded in 9-11 seconds.
Images not big . max width or height is 400-450.
ofcourse i tell this function in cycle like this : for (int i =0;i<10;i++){image[i]=getImage(url);}
Can any tell how to best and faste show image in my app ?
regards, Peter.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自演自醉2024-12-05 11:00:43

您无法消除下载和解码图像所需的时间。数字“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

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

You cannot do away with the time required for downloading and decoding images. The number '10' is just a function on the quality of the image and you can only try to optimize on this number.

If the server is managed by you, you might want to spend some time optimizing on the size of the downloadable images given your UI requirements. Also try lazy-loading (I hope you are not performing these operations on the UI thread). Many solutions for lazy-downloading and lazy-decoding have been discussed many times: 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: The usage of HttpURLConnection is discouraged. Use the HttpClient. This might also affect performance. Take a look at http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/

摘星┃星的人2024-12-05 11:00:43
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;
}
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;
}
深空失忆2024-12-05 11:00:43
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;
}
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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文