如何从存储在SD卡上的图像图像路径图像、路径、卡上、SD

2023-09-13 01:36:37 作者:抬头看、多皎洁的月光

是有可能得到的所有存储的我的Andr​​oid手机的SD卡上的图像的路径?也可以检查存储在SD卡或内部存储器中的图像吗?我目前在做这样的:

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 the internal memory? I am currently doing this:

Cursor cur = this.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null,null);  
        startManagingCursor(cur);  
        cur.moveToFirst();
        while (cur.moveToNext()) {
            String str = cur.getString(cur.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME));
            Log.e("#########",str);
        }

这好像返回目录名, 我也看到了这个code:

this seems as if returns the directories names, also I have seen this code:

File images = Environment.getDataDirectory();  
        File[] imagelist = images.listFiles(new FilenameFilter(){  
        public boolean accept(File dir, String name)  
        {  
            return ((name.endsWith(".jpg"))||(name.endsWith(".png")));  
        }  
    });  
        String []mFiles = new String[imagelist.length];  

        for(int i= 0 ; i< imagelist.length; i++)  
        {  
            mFiles[i] = imagelist[i].getAbsolutePath();  
        }  
        Uri[] mUrls = new Uri[mFiles.length];  

        for(int i=0; i < mFiles.length; i++)  
        {  
            mUrls[i] = Uri.parse(mFiles[i]);  
            Log.e("###############","MURIS: "+mUrls[i].getEncodedPath());
        }     

但这是抛出一个空指针异常。

but this is throwing a nullpointer exception .

推荐答案

您可以使用FileFilter这一点。下面是我写的一个用于返回包含图像的目录列表。从这一点,应该是相当简单的修改它返回图像的列表:

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

编辑:为了澄清,要利用这一点,你会做类似下面的:

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

File extStore = Environment.getExternalStorageDirectory();
File[] imageDirs = extStore.listFiles(filterForImageFolders);