Java 在调用Android BitmapFactory decodeFile之前调用了它
我试图在列表视图中显示图像的缩略图。这些图像位于我的SD卡上的Camera文件夹中。我正在使用BitmapFactory.decode文件读取图像。我想在解码文件时显示ProgressDialog。我试图先显示ProgressDialog,然后在for循环中调用decodeFile。在for循环之后,才会显示ProgressDialog。在显示ProgressDialog之前,对decodeFile的调用似乎正在运行 如何在for循环之前显示ProgressDialog 公共类ActivityProgressBar扩展ListActivity {Java 在调用Android BitmapFactory decodeFile之前调用了它,java,android,bitmapfactory,Java,Android,Bitmapfactory,我试图在列表视图中显示图像的缩略图。这些图像位于我的SD卡上的Camera文件夹中。我正在使用BitmapFactory.decode文件读取图像。我想在解码文件时显示ProgressDialog。我试图先显示ProgressDialog,然后在for循环中调用decodeFile。在for循环之后,才会显示ProgressDialog。在显示ProgressDialog之前,对decodeFile的调用似乎正在运行 如何在for循环之前显示ProgressDialog 公共类ActivityP
private Vector fileDataDisplay=null;
RowFileData文件数据;
ProgressDialog=null;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
progressDialog=新建progressDialog(此);
setMessage(“加载缩略图…”);
fileDataDisplay=新向量();
File currentDirectory=新文件(“/mnt/sdcard/dcim/camera”);
文件[]currentDirectoryFileList=currentDirectory.listFiles();
BitmapFactory.Options opts=新的BitmapFactory.Options();
opts.inSampleSize=16;
progressDialog.show();
对于(inti=0;i,我建议查看延迟加载您的图像。另一篇SO文章中有一个很好的例子,我刚刚实现了这个确切的目的,效果非常好
您也可以使用asynctask在后台解码位图,并在前面显示进度对话框。完成解码位图后,只需禁用进度对话框。并帮助您。Thnx。我希望在解码位图文件时显示进度对话框并保持显示。此外,我没有意识到Android中的e主线程是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;
}
}
private Vector fileDataDisplay=null;
RowFileData文件数据;
ProgressDialog=null;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
progressDialog=新建progressDialog(此);
setMessage(“加载缩略图…”);
fileDataDisplay=新向量();
File currentDirectory=新文件(“/mnt/sdcard/dcim/camera”);
BitmapFactory.Options opts=新的BitmapFactory.Options();
opts.inSampleSize=16;
文件[]currentDirectoryFileList=currentDirectory.listFiles();
progressDialog.show();
读取缩略图(currentDirectoryFileList,选项);
}
私有void readThumbnails(最终文件[]currentDirectoryFileList,最终位图工厂。选项选项选项)
{
线程背景线程=新线程()
{
公开募捐
{
尝试
{
对于(int i=0;iThanks),该教程非常有用。我没有意识到Android中的主线程是UI线程!我希望进度对话框会首先显示,并在位图文件解码时保持显示。我在一本名为Pro Android的书中找到了一个很好的示例。我将在下面发布我的最终答案。
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;
}
}