2011-08-26 53 views
0

我想顯示ListView中的圖像的縮略圖。這些圖像位於我的SD卡上的相機文件夾中。我正在使用BitmapFactory.decodeFile來讀取圖像。我想在文件解碼時顯示ProgressDialog。我想先顯示ProgressDialog,然後在for循環中調用decodeFile。 ProgressDialog直到for循環之後才顯示。在顯示ProgressDialog之前,對decodeFile的調用似乎正在運行。Android的BitmapFactory 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; 
    } 
} 

}

我爲了覈實後的for循環正在顯示的ProgressDialog註釋掉調用progressDialog.dismiss() 。我已經刪除了代碼行來顯示ListView中的圖像以便於閱讀。我證實我仍然在我的日誌for循環。

感謝

回答

1

您也可以使用背景位圖解碼和的AsyncTask顯示前面進度對話框。完成解碼位圖後,只需禁用進度對話框。 Android - AsyncTaskTutorial幫助你。日Thnx。

+0

感謝,該教程是非常有益的。我沒有意識到Android中的主線程是UI線程!我希望進度對話框會首先顯示,並在位圖文件解碼時保持顯示。我在一本名爲Pro Android的書中找到了一個很好的例子。我會在下面發表我的最終答案。 –

1

我建議尋找到延遲加載圖像。在另一個SO帖子中有一個很好的例子,我剛剛爲了這個確切的目的而實施了它,並且它很棒。

Lazy load of images in ListView

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; 
    } 
} 

}

相關問題