嗨我正在開發android畫廊應用程序。我從SD卡中的文件夾獲取的圖像,並在網格視圖中顯示它下面列出SD卡中的所有圖像。
public static ArrayList<String> getFilePaths()
{
ArrayList<String> filePaths = new ArrayList<String>();
File directory = new File(android.os.Environment.getExternalStorageDirectory() + File.separator + AppConstant.PHOTO_ALBUM);
// check for directory
if (directory.isDirectory())
{
// getting list of file paths
File[] listFiles = directory.listFiles();
// Check for count
if (listFiles.length > 0)
{
for (int i = 0; i < listFiles.length; i++)
{
String filePath = listFiles[i].getAbsolutePath();
if (IsSupportedFile(filePath))
{
// Add image path to array list
filePaths.add(filePath);
}
}
}
else
{
// image directory is empty
Toast.makeText(
_context,
AppConstant.PHOTO_ALBUM
+ " is empty. Please load some images in it !",
Toast.LENGTH_LONG).show();
}
}
return filePaths;
}
//fetching all image paths
imagePaths = utils.getFilePaths();
adapter = new GridViewImageAdapter(GridViewActivity.this, imagePaths, columnWidth);
// setting grid view adapter
gridView.setAdapter(adapter);
我想不僅在指定的文件夾,顯示所有從SD卡圖像。我不知道該怎麼做。
請幫忙。謝謝!
您只能使用URI而不是文件路徑。其更好的方法來從SD卡中獲取圖像 –
http://mobilecomputing650003.wordpress.com/2013/10/15/mediastore-content-provider-to-retrieve-images-from-sdcard-and-displaying-in-gridview/ 。檢查它是否有幫助 – Raghunandan
更改爲'File directory = Environment.getExternalStorageDirectory();'。我認爲這應該工作。 –