java - Android BitmapFactory解码文件在它应该被调用之前被调用

标签 java android bitmapfactory

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

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

公共(public)类 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() 的调用,以验证 ProgressDialog 是否在 for 循环后显示。我删除了在 ListView 中显示图像的代码行,以便于阅读。我确认我的日志仍在 for 循环中。

谢谢

最佳答案

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

关于java - Android BitmapFactory解码文件在它应该被调用之前被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7208925/

相关文章:

android - 为什么 BitmapFactory.decodeStream 返回 null?

java - 简单的 Java 客户端/服务器程序

android - 如何使用 Holo Theme 更改 Android 中按钮的背景颜色?

android - 使用 Bitmap.Config.RGB_565 将内存中的位图转换为位图

android - 从通知栏返回下载 Activity

android - 安卓模拟器的问题。不更新 xml 文件中的更改

android - 图片/jpeg 的 Base64 解码;android 中的 base64

java - 文件操作库

java - 如何在 Spring 和 JPA/Hibernate 中实现工作单元?

java - 在 Java 中为 CustomButton 创建 ActionEvent 对象