Android BitmapFactory decodeFile应该在它之前被调用

时间:2011-08-26 18:15:45

标签: java android bitmapfactory

我正在尝试在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中显示图像以便于阅读。我用我的Log验证了我还在for循环中。

由于

3 个答案:

答案 0 :(得分:1)

我建议您选择Lazy加载图片。在另一个SO帖子上有一个很好的例子,我刚刚为这个目的实现了它并且效果很好。

Lazy load of images in ListView

答案 1 :(得分:1)

您还可以在后台使用asynctask解码位图并在前面显示进度对话框。完成解码位图后,只需禁用进度对话框。 Android - AsyncTaskTutorial会帮助您。日Thnx。

答案 2 :(得分: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;
   }
}

}