在SD卡上的文件夹图片 [英] Images from folder on sd card

查看:112
本文介绍了在SD卡上的文件夹图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要由在SD卡(SD卡/文件夹名/filename.jpg应用程序创建的文件夹读取图像的应用程序。我不知道该文件的名称是因为用户指定该文件的名称。我需要阅读的文件夹中的图像及制作类似的默认图片查看器。即时通讯思想读成一个网格视图第一,但1)无法弄清楚如何动态地从文件夹2阅读)怎么样我想实现的是类似的默认浏览器中的图像选项?如果有一种方法来打开一个特定的文件夹,这将有助于默认的浏览器。 任何投入将是惊人的一直在努力了一段时间。 谢谢

I have an application that needs to read images from a folder created by the application on the sdcard(sdcard/"foldername"/"filename.jpg". I have no idea what the names of the files are because the user specifies the names of the files. I need to read the images from the folder and make something like the default image viewer. Im thinking read them into a grid view first but 1) cant figure out how to dynamically read them from a folder 2) how would I implement the image options like the default viewer? If there was a way to open the default viewer on a certain folder that would help. any input would be amazing been working on it for a while. Thanks

推荐答案

下面是你如何能得到一个文件夹列表关闭存储卡的:

Here's how you can get a list of folders off of the memory card:

String state = Environment.getExternalStorageState();
if(state.contentEquals(Environment.MEDIA_MOUNTED) || state.contentEquals(Environment.MEDIA_MOUNTED_READ_ONLY)) 
{
    String homeDir = Environment.getExternalStorageDirectory();
    File file = new File(homeDir);
    File[] directories = file.listFiles();
} 
else 
{
    Log.v("Error", "External Storage Unaccessible: " + state);
}

这code是从我的头顶,所以一些语法可能关闭了一点,但总体思路应该工作。你可以使用这样的事情来筛选下来的文件夹只包含图片的文件夹:

This code is from the top of my head, so some syntax may be off a bit, but the general idea should work. You can use something like this to filter down the folders to only folders that contain 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; 
            } 
        } 
    };

然后,第一示例更改为:

Then, change the first example to:

文件[]目录= file.listFiles(filterForImageFolders);

这应该只返回包含图像的目录。希望这有助于一些!

That should return only directories that contain images. Hopefully this helps some!

这篇关于在SD卡上的文件夹图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆