jhon
jhon

Reputation: 1

android Reading all the Directories in SDCARD which has images

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

                    }
                }       
            }
        }
    }
}

Upvotes: 0

Views: 2549

Answers (2)

Bharat
Bharat

Reputation: 121

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

Upvotes: 0

Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53657

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

Upvotes: 2

Related Questions