0

I am trying to display thumbnails of images in a ListView. The images are located in the Camera folder on my SDCard. I am using BitmapFactory.decodeFile to read in the images. I would like to display a ProgressDialog while the files are being decoded. I am trying to show the ProgressDialog first and then call decodeFile in a for loop. The ProgressDialog is not being displayed until after the for loop. The call to decodeFile appears to be running before the ProgressDialog is displayed.

How can I display the ProgressDialog before my for loop?

public class ActivityProgressBar extends ListActivity {

private Vector<RowFileData> fileDataDisplay = null;     
RowFileData fileData;
ProgressDialog progressDialog = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);        
    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Loading thumbnails...");
    fileDataDisplay = new Vector<RowFileData>();
    File currentDirectory = new File("/mnt/sdcard/dcim/camera");
    File[] currentDirectoryFileList = currentDirectory.listFiles();
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = 16;
    progressDialog.show();
    for(int i=0; i<currentDirectoryFileList.length; i++)
    {
        File currentDirectoryFile = currentDirectoryFileList[i];
        fileData = new RowFileData(BitmapFactory.decodeFile(currentDirectoryFile.getPath(), opts), currentDirectoryFile.getPath());
        fileDataDisplay.add(fileData);
        Log.v("myLog", "inside for loop");
    }
}   

private class RowFileData 
{
   protected Bitmap rowBitmap;
   protected String rowFileName;
   RowFileData(Bitmap bitmapPreview, String fileName)
   {
       rowBitmap = bitmapPreview;
       rowFileName = fileName;
   }
}

}

I've commented out the call to progressDialog.dismiss() in order to verify that the ProgressDialog was being shown after the for loop. I've removed the lines of code to display the images in the ListView for easier reading. I verified I was still in the for loop with my Log.

Thanks

Lou Morda
  • 5,078
  • 2
  • 44
  • 49

3 Answers3

1

I would suggest looking in to Lazy Loading your images. There is a great example on another SO post, which I've just implemented for this exact purpose and it works great.

Lazy load of images in ListView

Community
  • 1
  • 1
hooked82
  • 6,336
  • 4
  • 41
  • 46
1

You can also using asynctask for decode bitmap in background and display progress dialog in front. After completing the decode bitmap just disable the progress dialog. Android - AsyncTask and Tutorial help you. Thnx.

user370305
  • 108,599
  • 23
  • 164
  • 151
  • Thanks, the tutorial was very helpful. I did not realize that the main thread in Android is the UI thread! I was hoping the progress dialog would be displayed first and would stay displayed while the bitmap files were decoded. I found a great example in a book called Pro Android. I'll post my final answer below. – Lou Morda Aug 30 '11 at 21:40
0

I was hoping the progress dialog would be displayed and remain displayed while the bitmap files were decoded. Also, I did not realize that the main thread in Android is the UI thread, and the UI is frozen while processing occurs on the main thread! I used a background thread and now the progress dialog is displayed correctly:

public class ActivityProgressBar extends ListActivity {

private Vector<RowFileData> fileDataDisplay = null;     
RowFileData fileData;
ProgressDialog progressDialog = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list);        
    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Loading thumbnails...");
    fileDataDisplay = new Vector<RowFileData>();
    File currentDirectory = new File("/mnt/sdcard/dcim/camera");
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inSampleSize = 16;
    File[] currentDirectoryFileList = currentDirectory.listFiles();
    progressDialog.show();
    readThumbnails(currentDirectoryFileList, opts);
}   

private void readThumbnails(final File[] currentDirectoryFileList, final BitmapFactory.Options opts) 
{
    Thread backgroundThread = new Thread()
    {
        public void run()
        {
            try
            {
                for(int i=0; i<currentDirectoryFileList.length; i++)
                {
                    File currentDirectoryFile = currentDirectoryFileList[i];
                    fileData = new RowFileData(BitmapFactory.decodeFile(currentDirectoryFile.getPath(), opts), currentDirectoryFile.getPath());
                    fileDataDisplay.add(fileData);
                    Log.v("myLog", "inside for loop");
                }
                uiCallback.sendEmptyMessage(0);
            }
            catch(Exception ex)
            {
                Log.v("myLog", ex.toString());
            }
        }
    };
    backgroundThread.start();
}

private Handler uiCallback = new Handler()
{
    @Override
    public void handleMessage(Message emptyMessage)
    {
        progressDialog.dismiss();
    }
};

private class RowFileData 
{
   protected Bitmap rowBitmap;
   protected String rowFileName;
   RowFileData(Bitmap bitmapPreview, String fileName)
   {
       rowBitmap = bitmapPreview;
       rowFileName = fileName;
   }
}

}

Lou Morda
  • 5,078
  • 2
  • 44
  • 49