列出从SD卡中的所有图像。 [英] List out all images from SD card.

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

问题描述

我开发Android画廊的应用程序。我从SD卡中的文件夹中获取的图像和网格视图中显示它,如下

Hi I am developing android Gallery app . I am fetching the images from a folder in sd card and displaying it on a grid view as below

public static ArrayList<String> getFilePaths()
{
    ArrayList<String> filePaths = new ArrayList<String>();

    File directory = new File(android.os.Environment.getExternalStorageDirectory() + File.separator + AppConstant.PHOTO_ALBUM);

    // check for directory
    if (directory.isDirectory())
    {
        // getting list of file paths
        File[] listFiles = directory.listFiles();

        // Check for count
        if (listFiles.length > 0) 
        {

            for (int i = 0; i < listFiles.length; i++)
            {

                String filePath = listFiles[i].getAbsolutePath();

                if (IsSupportedFile(filePath)) 
                {
                    // Add image path to array list
                    filePaths.add(filePath);
                }
            }
        } 
        else 
        {
            // image directory is empty
            Toast.makeText(
                    _context,
                    AppConstant.PHOTO_ALBUM
                    + " is empty. Please load some images in it !",
                    Toast.LENGTH_LONG).show();
        }

    } 
    return filePaths;
}


//fetching all image paths
 imagePaths = utils.getFilePaths();
 adapter = new GridViewImageAdapter(GridViewActivity.this, imagePaths, columnWidth);
 // setting grid view adapter
 gridView.setAdapter(adapter);

我要显示从SD卡中的所有图片不仅是一个指定的文件夹中。我不知道怎么做。

I want to display all the images from SD card not only in a specified folder. I am not sure How to do it.

请帮助。谢谢!

推荐答案

使用此方法。这将返回列表中的所有图片的路径您的SD卡里面,如果你不想让任何指定的图像扩展,你可以筛选出来。

Use this Method. This will return list all the images path inside your sdcard, and if you don't want any specified image extension you can filter that out.

public ArrayList<String> getFilePaths()
    {


        Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; 
        String[] projection = {MediaStore.Images.ImageColumns.DATA}; 
        Cursor c = null;
        SortedSet<String> dirList = new TreeSet<String>();
        ArrayList<String> resultIAV = new ArrayList<String>();

         String[] directories = null; 
        if (u != null) 
        { 
            c = managedQuery(u, projection, null, null, null); 
        } 

        if ((c != null) && (c.moveToFirst())) 
        { 
            do 
            {
                String tempDir = c.getString(0);
                tempDir = tempDir.substring(0, tempDir.lastIndexOf("/"));
                try{
                    dirList.add(tempDir);
                }
                catch(Exception e)
                {

                }
            } 
            while (c.moveToNext());
            directories = new String[dirList.size()];
            dirList.toArray(directories);

        }

        for(int i=0;i<dirList.size();i++)
        {
            File imageDir = new File(directories[i]);
            File[] imageList = imageDir.listFiles();
            if(imageList == null)
                continue;
            for (File imagePath : imageList) { 
                try {

                        if(imagePath.isDirectory())
                        {
                            imageList = imagePath.listFiles();

                        }
                        if ( imagePath.getName().contains(".jpg")|| imagePath.getName().contains(".JPG")  
                                || imagePath.getName().contains(".jpeg")|| imagePath.getName().contains(".JPEG")                                    
                                || imagePath.getName().contains(".png") || imagePath.getName().contains(".PNG")
                                || imagePath.getName().contains(".gif") || imagePath.getName().contains(".GIF")
                                || imagePath.getName().contains(".bmp") || imagePath.getName().contains(".BMP")                         
        )
                        {



                            String path= imagePath.getAbsolutePath();
                        resultIAV.add(path);

                        }
                    }
            //  }
            catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        return resultIAV;


    }

这篇关于列出从SD卡中的所有图像。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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