开发者

image from url to drawable or bitmap :best and fastest way

开发者 https://www.devze.com 2023-03-26 10:00 出处:网络
im tried to show images from url in my app. But ways which im using is very long . this code i founded on stackoverflow

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 = (Http开发者_Go百科URLConnection)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.


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/


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号