0

ALL,

I have a following problem. Here is the code:

 class ParseCommunicator
 {
    private static int width = -1;
    private static int height = -1;

@SuppressWarnings("deprecation")
public void getPhoto(Device dev) throws ParseException, IOException
{
    InputStream stream = null;
    Bitmap bmp = null;
    BitmapFactory.Options opts = new BitmapFactory.Options();
    Rect rect = new Rect();
    WindowManager wm = (WindowManager) context.getSystemService( Context.WINDOW_SERVICE );
    Display display = wm.getDefaultDisplay();
    width = display.getWidth();
    height = display.getHeight();
    try
    {
        stream = new URL( dev.getBitmapURL() ).openStream();
        opts.inJustDecodeBounds = true;
        bmp = BitmapFactory.decodeStream( stream, rect, opts );
        int bmpHeight = opts.outHeight;
        int bmpWidth = opts.outWidth;
        int inSampleSize = 1;
        int reqHeight = height / 3 ;
        int reqWidth = width / 2;
        if( bmpHeight > reqHeight || bmpWidth > reqWidth )
        {
            int halfHeight = bmpHeight / 2;
            int halfWidth = bmpWidth / 2;
            while( ( halfHeight / inSampleSize ) > reqHeight && ( halfWidth / inSampleSize ) > reqWidth )
                inSampleSize *= 2;
        }
        opts.inSampleSize = inSampleSize;
        opts.inJustDecodeBounds = false;
        bmp = BitmapFactory.decodeStream( stream, rect, opts );
    }
    catch( MalformedURLException e )
    {
        e.printStackTrace();
    }
    finally
    {
        if( stream != null )
            stream.close();
    }
    if( bmp != null )
        dev.setPhoto( bmp );
}

This code should get the bitmap (stored as png) from the Parse interface. When running the code it does not give me a bitmap, it has NULL in that object. No exception is thrown.

Trying to debug I found out following:

If I take out sampling, the bitmap is read without any issues, i.e. commenting the lines:

opts.inJustDecodeBounds = true;
...............
opts.inJustDecodeBounds = false;

will produce the bitmap I am looking for.

Those bitmaps will be later on used in a grid view.

The URL it is reading from is correct as I can get the bitmap without any issues without sampling.

The same sampling code works fine when I try to sample the picture taken from the Android Gallery and put it on the ImageView.

Could someone spot what is going on?

I'm doing my testing on the LG Android phone with Android 2.2.

Thank you in advance.

Igor
  • 5,620
  • 11
  • 51
  • 103
  • 1
    I've had this problem before (not with Parse) - the solution was to reset the InputStream before reading the second time: http://stackoverflow.com/a/17163300/833647 – Ken Wolf Apr 12 '14 at 07:57
  • @KenWolf, thank you. Works like a charm. Now I just wonder if it documented anywhere? – Igor Apr 12 '14 at 08:12

1 Answers1

1

The problem is you are using the same inputstream twice in BitmapFactory.decodeStream().

Try calling reset() before you want to load the bitmap "for real"

// First make sure you are using a BufferedInputStream
InputStream bis = new BufferedInputStream(stream)

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bis.reset();

Here's a nice explanation from another user: https://stackoverflow.com/a/11214451/833647

Community
  • 1
  • 1
Ken Wolf
  • 23,133
  • 6
  • 63
  • 84
  • Thank you for the link and explanation. Now I can mark this in a comment in my own code to not forget about it in the future. – Igor Apr 12 '14 at 08:22