Reputation: 278
As far as i know
File directory = new File(
android.os.Environment.getExternalStorageDirectory()
+ File.separator + "100ANDRO");
will get the image directory, but i am afraid if some phones have different directory then it wont work, is there any direct way that android api provides us to get all the image paths without hard coding the directory name as 100ANDRO ?
Thanks in advance.
Upvotes: 3
Views: 2526
Reputation: 1710
ArrayList fileList = new ArrayList();
// Pass directory path to this function and it will return the files
public ArrayList<File> getfile(File dir) {
File listFile[] = dir.listFiles();
if (listFile != null && listFile.length > 0) {
for (int i = 0; i < listFile.length; i++) {
if (listFile[i].isDirectory()) {
// fileList.add(listFile[i]);
getfile(listFile[i]);
} else {
if(listFile[i].getName().endsWith(".jpg")
|| listFile[i].getName().endsWith(".png")
|| listFile[i].getName().endsWith(".jpeg")
|| listFile[i].getName().endsWith(".gif")
)
{
fileList.add(listFile[i]);
}
}
}
}
return fileList;
}
Upvotes: 0
Reputation: 23638
You can use MediaStore.Images.Media.EXTERNAL_CONTENT_URI
which is only the external storage.For the internal there is MediaStore.Images.Media.INTERNAL_CONTENT_URI
. You can use a MergeCursor to combine both query results.
The main thing is to make use of the MediaStore
class, which is a Media provider
that contains data for all available media on both internal and external storage devices (such as an SD card). An adapter is used as a bridge between the data and the view.
For the implementation check out Demo Fetch Images from SDcard and display in GridView
Upvotes: 1