How to get image path from images stored on sd card

后端 未结 1 1212
时光取名叫无心
时光取名叫无心 2021-01-13 12:29

is it possible to get the path of all the images that are stored on the sd card of my android phone? also is it possible to check for other images stored on sd card or in th

相关标签:
1条回答
  • 2021-01-13 13:26

    You can use a FileFilter for this. Here's one I wrote for returning a list of directories that contain images. From this, it should be fairly simple to modify it to return a list of images:

    FileFilter filterForImageFolders = new FileFilter() 
        {            
            public boolean accept(File folder) 
            { 
                try 
                { 
                    //Checking only directories, since we are checking for files within 
                    //a directory 
                    if(folder.isDirectory()) 
                    { 
                        File[] listOfFiles = folder.listFiles(); 
    
                        if (listOfFiles == null) return false; 
    
                        //For each file in the directory... 
                        for (File file : listOfFiles) 
                        {                            
                            //Check if the extension is one of the supported filetypes                           
                            //imageExtensions is a String[] containing image filetypes (e.g. "png")
                            for (String ext : imageExtensions) 
                            { 
                                if (file.getName().endsWith("." + ext)) return true; 
                            } 
                        }                        
                    } 
                    return false; 
                } 
                catch (SecurityException e) 
                { 
                    Log.v("debug", "Access Denied"); 
                    return false; 
                } 
            } 
        };
    

    EDIT: To clarify, to use this, you would do something like the below:

    File extStore = Environment.getExternalStorageDirectory();
    File[] imageDirs = extStore.listFiles(filterForImageFolders);
    
    0 讨论(0)
提交回复
热议问题