From 75288f635132b98b366e6993be945f3c9ddf8f05 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 20 Feb 2016 15:44:47 +0100 Subject: [PATCH] create a list of directories with theirhoto count --- .../simplemobiletools/com/Directory.java | 25 +++++++++++++ .../simplemobiletools/com/MainActivity.java | 35 +++++++++++++++---- 2 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/gallery/simplemobiletools/com/Directory.java diff --git a/app/src/main/java/gallery/simplemobiletools/com/Directory.java b/app/src/main/java/gallery/simplemobiletools/com/Directory.java new file mode 100644 index 000000000..730a4b94a --- /dev/null +++ b/app/src/main/java/gallery/simplemobiletools/com/Directory.java @@ -0,0 +1,25 @@ +package gallery.simplemobiletools.com; + +public class Directory { + private final String path; + private final String name; + private final int photoCnt; + + public Directory(String path, String name, int photoCnt) { + this.path = path; + this.name = name; + this.photoCnt = photoCnt; + } + + public String getPath() { + return path; + } + + public String getName() { + return name; + } + + public int getPhotoCnt() { + return photoCnt; + } +} diff --git a/app/src/main/java/gallery/simplemobiletools/com/MainActivity.java b/app/src/main/java/gallery/simplemobiletools/com/MainActivity.java index f1ea3dadf..c8822016d 100644 --- a/app/src/main/java/gallery/simplemobiletools/com/MainActivity.java +++ b/app/src/main/java/gallery/simplemobiletools/com/MainActivity.java @@ -1,32 +1,55 @@ package gallery.simplemobiletools.com; -import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + public class MainActivity extends AppCompatActivity { + private List dirs; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - getImages(); + + Map directories = getImages(); + dirs = new ArrayList<>(directories.size()); + + for (Map.Entry dir : directories.entrySet()) { + final String path = dir.getKey(); + final String name = path.substring(path.lastIndexOf("/") + 1); + final int cnt = dir.getValue(); + dirs.add(new Directory(path, name, cnt)); + } } - private void getImages() { - final ContentResolver contentResolver = getContentResolver(); + private Map getImages() { + Map dirs = new HashMap<>(); final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; - final Cursor cursor = contentResolver.query(uri, null, null, null, null); + final Cursor cursor = getContentResolver().query(uri, null, null, null, null); if (cursor != null && cursor.moveToFirst()) { final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); do { - final String path = cursor.getString(pathIndex); + final File file = new File(cursor.getString(pathIndex)); + final String path = file.getParent().toLowerCase(); + if (dirs.containsKey(path)) { + dirs.put(path, dirs.get(path) + 1); + } else { + dirs.put(path, 1); + } } while (cursor.moveToNext()); cursor.close(); } + + return dirs; } }