Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I want to defer the reading of a bitmap to another thread. I'm mainly concerned about concurrency issues since I'm kind of green on that subject, so I would like to know if this code has any potential flaws.

public class SomeActivity extends Activity {

    Bitmap threadBitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        new BitmapTask().execute();
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if(threadBitmap != null) {
                    // Assume bitmap has loaded and do something with it here
                }
            }

        });
    } 

    public class BitmapTask extends AsyncTask<Void, Void, Bitmap> {

        @Override
        protected Bitmap doInBackground(Void... params) {
            Bitmap bitmap = null;
            InputStream in = null;

            try {
                in = getAssets().open("mybitmap.png");
                bitmap = BitmapFactory.decodeStream(in);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if(in != null) {
                        in.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap result) {
            threadBitmap = result;
        }

    }

}
share|improve this question

2 Answers

up vote 2 down vote accepted

When you start reading of a bitmap?

  1. If it starting in onCreate, you should use a progress bar while you load resources.

  2. If it starting after some user interaction you can use default image for button and set new image in onPostExecute() methods. Your button should be class instance variable of course.

  3. And try block inside finally is ugly.

share|improve this answer
Ugly, but necessary I guess? How else would you close the InputStream? Thanks for the answer! – soren.qvist Oct 21 '12 at 13:52
After BitmapFactory.decodeStream(in); Why not? – scame Oct 22 '12 at 19:06
Well if the BitmapFactory.decodeStream(in); throws an exception then in.close() won't get called. – soren.qvist Oct 22 '12 at 23:06
BitmapFactory.decodeStream – scame Oct 23 '12 at 6:45
Well I can't argue with facts :) It just seemed natural to me that it would throw an exception if the stream didn't represent a bitmap. Thanks! – soren.qvist Oct 24 '12 at 17:07

I would implement a progress bar to inform the user of the image loading status to eliminate confusion to what it's going on.

Also, you could deactivate the button for as long as the thread is running. And reactivate it in onPostExecute().

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.