3

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.

4

2 回答 2

1

您可以使用MediaStore.Images.Media.EXTERNAL_CONTENT_URI仅外部存储。对于内部有MediaStore.Images.Media.INTERNAL_CONTENT_URI. 您可以使用MergeCursor组合两个查询结果。

主要是利用MediaStore类,它Media provider包含内部和外部存储设备(例如 SD 卡)上所有可用媒体的数据。适配器用作数据和视图之间的桥梁。

对于实施检查 Demo Fetch Images from SDcard 并显示在 GridView

于 2013-10-15T05:44:01.590 回答
0
    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;
        }
于 2017-02-24T11:05:27.543 回答