Search code examples
javaandroidbitmapfactory

Android BitmapFactory decodeFile is called before it should be


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


Solution

  • 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.