Monday, December 10, 2012

Re: [android-developers] Issues with get large image form url

You should first of all download that image and store it in a file instead of directly decoding a 2MB stream (which depending of the compression degree will of course explode to a much larger size in memory). From that file you can load a smaller version of that image which fits into the view that is going to display the bitmap. That's the clean way to avoid your out of memory problems.

By the way, what you're trying to do in your second method called getBitmapFromInputStream most likely cannot work. You are trying to read from the same stream twice, first of all for determining the bitmap size, the second time for decoding the actual bitmap. In between these two read accesses the stream should be reset to its beginning. However, this is not guaranteed to work. Some stream implementations are "one way only". Once it is read there is no way back.

On Monday, December 10, 2012 9:42:37 AM UTC-6, snowdream wrote:
Hi,everyone.
    if i want to get a large image (eg.a image which size is  2M or larger.)from url, i use the folllowing code ,and get the exception ."Out of Memory".
    Any good idea?
   Thank you.


    private Bitmap getBitmapFromUrl(String url) {
        Bitmap bitmap = null;

        try {
            URL imagePath = new URL(url);
            HttpURLConnection conn = (HttpURLConnection)imagePath.openConnection();
            conn.setConnectTimeout(CONNECT_TIMEOUT);
            conn.setReadTimeout(READ_TIMEOUT);
            conn.connect();
            InputStream is = (InputStream) conn.getContent();
            if (is != null) {
                // bitmap = getBitmapFromInputStream(is, width, height);
                bitmap = BitmapFactory.decodeStream((InputStream) conn.getContent());
            }
        } catch(Exception e) {
            e.printStackTrace();
        }
        
        return bitmap;
    }


    public static Bitmap getBitmapFromInputStream(InputStream is, int width, int height){
        Bitmap bitmap = null;
        if (null != is ) {
            BufferedInputStream bis = new BufferedInputStream(is); 

            BitmapFactory.Options opts = null;
            if (width > 0 && height > 0) {
                opts = new BitmapFactory.Options();
                opts.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(is, null, opts);
                
                // 计算图片缩放比例
                final int minSideLength = Math.min(width, height);
                opts.inSampleSize = BitmapUtils.computeSampleSize(opts.outWidth, opts.outHeight, minSideLength,
                        width * height);
                opts.inJustDecodeBounds = false;
                opts.inInputShareable = true;
                opts.inPurgeable = true;
            }
            
            try {
                bitmap =  BitmapFactory.decodeStream(bis, null, opts);
            } catch (OutOfMemoryError e) {
                e.printStackTrace();
            }
        }
        return bitmap;
    }  




--
杨辉
Impossible is nothing!



------------------------------------------------------------------------------------
HDExplorer APK (A Nice File Manager,Simplicity but not simple.

Google Market:
https://market.android.com/details?id=com.hd.explorer

Google Code:
http://code.google.com/p/hdexplorer/

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment