From c2762ab014d57ce4f5ca60c9d0d467d6efeb6626 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 20 Feb 2016 17:20:27 +0100 Subject: [PATCH] use the first image in the directory as thumbnail --- .../simplemobiletools/com/Directory.java | 18 ++++++---- .../com/DirectoryAdapter.java | 2 +- .../simplemobiletools/com/MainActivity.java | 35 ++++++++----------- 3 files changed, 26 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/gallery/simplemobiletools/com/Directory.java b/app/src/main/java/gallery/simplemobiletools/com/Directory.java index 53dd4b6f0..136d1ee5a 100644 --- a/app/src/main/java/gallery/simplemobiletools/com/Directory.java +++ b/app/src/main/java/gallery/simplemobiletools/com/Directory.java @@ -1,25 +1,29 @@ package gallery.simplemobiletools.com; public class Directory { - private final String path; + private final String thumbnail; private final String name; - private final String photoCnt; + private int photoCnt; - public Directory(String path, String name, String photoCnt) { - this.path = path; + public Directory(String thumbnail, String name, int photoCnt) { + this.thumbnail = thumbnail; this.name = name; this.photoCnt = photoCnt; } - public String getPath() { - return path; + public String getThumbnail() { + return thumbnail; } public String getName() { return name; } - public String getPhotoCnt() { + public int getPhotoCnt() { return photoCnt; } + + public void setPhotoCnt(int cnt) { + photoCnt = cnt; + } } diff --git a/app/src/main/java/gallery/simplemobiletools/com/DirectoryAdapter.java b/app/src/main/java/gallery/simplemobiletools/com/DirectoryAdapter.java index 201353d14..67322cbf8 100644 --- a/app/src/main/java/gallery/simplemobiletools/com/DirectoryAdapter.java +++ b/app/src/main/java/gallery/simplemobiletools/com/DirectoryAdapter.java @@ -31,7 +31,7 @@ public class DirectoryAdapter extends BaseAdapter { Directory dir = dirs.get(position); holder.dirName.setText(dir.getName()); - holder.photoCnt.setText(dir.getPhotoCnt()); + holder.photoCnt.setText(String.valueOf(dir.getPhotoCnt())); return view; } diff --git a/app/src/main/java/gallery/simplemobiletools/com/MainActivity.java b/app/src/main/java/gallery/simplemobiletools/com/MainActivity.java index aa5b62975..98bdc81ec 100644 --- a/app/src/main/java/gallery/simplemobiletools/com/MainActivity.java +++ b/app/src/main/java/gallery/simplemobiletools/com/MainActivity.java @@ -9,36 +9,24 @@ import android.widget.GridView; import java.io.File; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; public class MainActivity extends AppCompatActivity { - private List dirs; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - - final Map directories = getImages(); - dirs = new ArrayList<>(directories.size()); - - for (Map.Entry dir : directories.entrySet()) { - final String path = dir.getKey(); - final String dirName = path.substring(path.lastIndexOf("/") + 1); - final String cnt = String.valueOf(dir.getValue()); - dirs.add(new Directory(path, dirName, cnt)); - } - final GridView gridView = (GridView) findViewById(R.id.photo_grid); - DirectoryAdapter adapter = new DirectoryAdapter(this, dirs); + + final List dirs = new ArrayList<>(getDirectories().values()); + final DirectoryAdapter adapter = new DirectoryAdapter(this, dirs); gridView.setAdapter(adapter); } - private Map getImages() { - final Map directories = new TreeMap<>(); + private Map getDirectories() { + final Map directories = new TreeMap<>(); final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; final Cursor cursor = getContentResolver().query(uri, null, null, null, null); @@ -46,11 +34,16 @@ public class MainActivity extends AppCompatActivity { final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); do { final File file = new File(cursor.getString(pathIndex)); - final String path = file.getParent().toLowerCase(); - if (directories.containsKey(path)) { - directories.put(path, directories.get(path) + 1); + final String fileDir = file.getParent().toLowerCase(); + + if (directories.containsKey(fileDir)) { + final Directory directory = directories.get(fileDir); + final int newImageCnt = directory.getPhotoCnt() + 1; + directory.setPhotoCnt(newImageCnt); } else { - directories.put(path, 1); + final String thumbnail = file.getAbsolutePath(); + final String dirName = fileDir.substring(fileDir.lastIndexOf("/") + 1); + directories.put(fileDir, new Directory(thumbnail, dirName, 1)); } } while (cursor.moveToNext()); cursor.close();