Igor
Igor

Reputation: 6255

Read the bitmap from Parse on Android

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.

Upvotes: 0

Views: 222

Answers (1)

Ken Wolf
Ken Wolf

Reputation: 23269

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

Upvotes: 1

Related Questions