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

发布于 2024-11-18 10:11:19 字数 1772 浏览 1 评论 0原文

我正在尝试读取 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();

                    }
                }       
            }
        }
    }
}

I am trying to read all the images in the SDCARD with the Directory in which its present. e.g: if there is a file TEST.jpg in /mnt/sdcard/album1 and TEST2.jpg in /mnt/sdcard/album1/album2 i should be able to get the directory name album1 and album2.
I have written a code which does this in recursive manner, This works when the no of folders are less but when the number of directories increases the loop just come out of it.

      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();

                    }
                }       
            }
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云仙小弟 2024-11-25 10:11:19

使用以下代码。
获取 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;
    }

Use the following code.
to get the path of all images and directories from 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;
    }
吹梦到西洲 2024-11-25 10:11:19

要从 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());
                }
            }   
        }
    }
}

To get all the image files from the Sdcard, it may work.

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());
                }
            }   
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文