android读取SDCARD中有图像的所有目录

时间:2011-07-01 10:06:03

标签: android directory sd-card

我正在尝试使用其存在的目录读取SDCARD中的所有图像。例如:如果/ mnt / sdcard / album1中的文件TEST.jpg和/ mnt / sdcard / album1 / album2中的TEST2.jpg我应该能够获得目录名album1和album2。 我已经编写了一个以递归方式执行此操作的代码,当没有文件夹数量较少但是当目录数量增加时,循环就会出现。

      public void getImageFoldes(String filepath){


            String albumpath;
            File file = new File(filepath);

    File[] files = file.listFiles();
    for (int fileInList = 0; fileInList < files.length; fileInList++)  
    {
        File filename;
        filename =files[fileInList];

        if(filename.isHidden()|| filename.toString().startsWith("."))
            return;

        if (filename.isDirectory()){

            albumpath = filename.toString();
            String[] split;
            String title;
            split= albumpath.split("/");
            title=split[split.length-1];
            result = new thumbnailResults();
            result.setTitle(title);
            result.setPath(albumpath);
            result.setIsLocal(true);
            //result.setCreated("05-06-2011");
            getImageFoldes(filename.toString());
        }
        else{
            if (files.length !=0)
            {
                //if File is the image file then store the album name 
                if ((files[fileInList].toString()).contains(".png")||
                        (files[fileInList].toString()).contains(".jpg")||
                        (files[fileInList].toString()).contains(".jpeg")){
                    if (!results.contains(result)){
                        result.setUri(Uri.parse(files[fileInList].getPath()));
                        results.add(result);
                        myadapter.notifyDataSetChanged();

                    }
                }       
            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

使用以下代码。 从sdcard获取所有图像和目录的路径。

public static ArrayList<String> getPathOfAllImages(Activity activity) {
        ArrayList<String> absolutePathOfImageList = new ArrayList<String>();
        String absolutePathOfImage = null;
        String nameOfFile = null;
        String absolutePathOfFileWithoutFileName = null;
        Uri uri;
        Cursor cursor;
        int column_index;
        int column_displayname;
        int lastIndex;
        // absolutePathOfImages.clear();


            uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

            String[] projection = { MediaColumns.DATA,
                    MediaColumns.DISPLAY_NAME };

            cursor = activity.managedQuery(uri, projection, null, null, null);
            column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);

            column_displayname = cursor
                    .getColumnIndexOrThrow(MediaColumns.DISPLAY_NAME);

            // cursor.moveToFirst();
            while (cursor.moveToNext()) {
                // for(int i=0; i<cursor.getColumnCount();i++){
                // Log.i(TAG,cursor.getColumnName(i)+".....Data Present ...."+cursor.getString(i));
                // }
                // Log.i(TAG,"=====================================");

                absolutePathOfImage = cursor.getString(column_index);
                nameOfFile = cursor.getString(column_displayname);

                lastIndex = absolutePathOfImage.lastIndexOf(nameOfFile);

                lastIndex = lastIndex >= 0 ? lastIndex
                        : nameOfFile.length() - 1;

                absolutePathOfFileWithoutFileName = absolutePathOfImage
                        .substring(0, lastIndex);


                    if (absolutePathOfImage != null) {
                        absolutePathOfImageList.add(absolutePathOfImage);
                    }

            }


        // Log.i(TAG,"........Detected images for Grid....."
        // + absolutePathOfImageList);
        return absolutePathOfImageList;
    }

答案 1 :(得分:0)

要从Sdcard获取所有图像文件,它可能会起作用。

public class ReadallImagesActivity extends Activity {

    ArrayList<String> arlist = new ArrayList<String>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        File ff = Environment.getExternalStorageDirectory();
        loadImagepaths(ff);
        setContentView(R.layout.main);
        Toast.makeText(ReadallImagesActivity.this, "Array size == " +arlist.size(), Toast.LENGTH_LONG).show();
    }

    public void loadImagepaths(File file) {
        for (File f : file.listFiles()) {

            if (f.isDirectory()) {
                if (f.getAbsolutePath().endsWith(".android_secure")) {
                    break;
                }
                if (f.getAbsolutePath().endsWith("DCIM")) {
                    continue;
                }
                loadImagepaths(f);
            } else {
                if (f.getAbsolutePath().endsWith(".png") ||
                    f.getAbsolutePath().endsWith(".gif") ||
                    f.getAbsolutePath().endsWith(".jpg"))
                {
                    arlist.add(f.getAbsolutePath());
                }
            }   
        }
    }
}