1
\$\begingroup\$

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;
        }

    }

}
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Ugly, but necessary I guess? How else would you close the InputStream? Thanks for the answer! \$\endgroup\$ – soren.qvist Oct 21 '12 at 13:52
  • \$\begingroup\$ After BitmapFactory.decodeStream(in); Why not? \$\endgroup\$ – scame Oct 22 '12 at 19:06
  • \$\begingroup\$ Well if the BitmapFactory.decodeStream(in); throws an exception then in.close() won't get called. \$\endgroup\$ – soren.qvist Oct 22 '12 at 23:06
  • \$\begingroup\$ BitmapFactory.decodeStream \$\endgroup\$ – scame Oct 23 '12 at 6:45
  • \$\begingroup\$ 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! \$\endgroup\$ – soren.qvist Oct 24 '12 at 17:07
2
\$\begingroup\$

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().

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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