0

我正在尝试在 ListView 中显示图像的缩略图。这些图像位于我的 SDCard 上的 Camera 文件夹中。我正在使用 BitmapFactory.decodeFile 读取图像。我想在文件被解码时显示一个 ProgressDialog。我试图先显示 ProgressDialog,然后在 for 循环中调用 decodeFile。直到 for 循环之后才会显示 ProgressDialog。在显示 ProgressDialog 之前,对 decodeFile 的调用似乎正在运行。

如何在我的 for 循环之前显示 ProgressDialog?

公共类 ActivityProgressBar 扩展 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;
   }
}

}

我已经注释掉了对 progressDialog.dismiss() 的调用,以验证在 for 循环之后显示 ProgressDialog。为了便于阅读,我删除了在 ListView 中显示图像的代码行。我用我的日志验证了我仍在 for 循环中。

谢谢

4

3 回答 3

1

我建议查看延迟加载您的图像。在另一篇 SO 帖子中有一个很好的例子,我刚刚实现了这个确切的目的并且效果很好。

ListView 中图像的延迟加载

于 2011-08-26T18:25:10.627 回答
1

您还可以使用 asynctask 在后台解码位图并在前面显示进度对话框。完成解码位图后,只需禁用进度对话框。Android - AsyncTask教程帮助你。谢谢。

于 2011-08-26T18:43:53.123 回答
0

我希望在位图文件被解码时会显示进度对话框并保持显示。另外,我没有意识到Android中的主线程是UI线程,并且在主线程上进行处理时UI被冻结!我使用了后台线程,现在进度对话框正确显示:

公共类 ActivityProgressBar 扩展 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;
   }
}

}

于 2011-08-30T21:43:13.620 回答