I have seen something similar when using the unbuffered variants of
stream readers when getting from urls.

Youre using a BufferedInputStream which might have the same effect. I
successfully use the following to get images into bitmaps and was
having similar problem until it was changed to use BufferedReader so
you might try refactoring a bit and see if it helps.

BufferedReader in = null;
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpResponse response = client.execute(request);
in = new BufferedReader (new
InputStreamReader(response.getEntity().getContent()));
return BitmapFactory.decodeStream(instream, null, decodeOptions);

On Oct 21, 1:49 pm, ryanm <ryanmat...@gmail.com> wrote:
> I have a simple function to get an image file from a remote server and
> return it as a bitmap, here:
>
>     public static Bitmap getRemoteImage(final URL aURL) {
>         try {
>
>             final URLConnection conn = aURL.openConnection();
>             conn.connect();
>
>                 InputStream is = conn.getInputStream();
>                 BufferedInputStream bis = new BufferedInputStream(is,
> (512*1024));
>
>                 ByteArrayBuffer baf = new ByteArrayBuffer(50);
>                 int current = 0;
>                 while ((current = bis.read()) != -1) {
>                     baf.append((byte)current);
>                 }
>                 byte[] imageData = baf.toByteArray();
>                 Bitmap bm = BitmapFactory.decodeByteArray(imageData, 0,
> imageData.length);
>
>                 bis.close();
>                 is.close();
>
>                 // Just to give me a break point after everything else in the
> function is done
>                 Bitmap b = bm;
>
>                 return b;
>         } catch (Exception e) {
>                 Exception xcp = e;
>                 xcp = null;
>         }catch(Error e){
>                 Error xcp = e;
>                 xcp = null;
>         }
>         return null;
>     }
>
> Everything in this function executes exactly as you would expect, the
> byte array is full with the right amount of data, the decodeByteArray
> method returns a Bitmap, both close functions work, and then instead
> of returning the bitmap, it simply bails out of the try block and
> returns null.
>
> No errors, no warnings, nothing. I've had the issues with OOM errors
> before and this is not throwing one. LogCat is not showing an
> allocation error as you would expect if it were a memory issue. The
> bitmap that is returned has properties that I can see, but not the
> correct ones. The bitmap object ("bm") looks like this:
>
> bm      Bitmap  (id=830063098568)
>         mDensity        160
>         mHeight -1
>         mIsMutable      false
>         mNativeBitmap   1348080
>         mNinePatchChunk null
>         mRecycled       false
>         mSrcName        null
>         mWidth  -1
>
> The byte array has a length of 73462, so I know it got the image. The
> image is a png, but I've tried several different formats. If I could
> get an error I would at least know what to fix, but it simply skips
> the return statement inside the try block and returns null every
> time.
>
> Anyone have any insight?
>
> ryanm

-- 
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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to