create a list of directories with theirhoto count

This commit is contained in:
tibbi 2016-02-20 15:44:47 +01:00
parent 3cafb49996
commit 75288f6351
2 changed files with 54 additions and 6 deletions

View file

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

View file

@ -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<Directory> dirs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getImages();
Map<String, Integer> directories = getImages();
dirs = new ArrayList<>(directories.size());
for (Map.Entry<String, Integer> 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<String, Integer> getImages() {
Map<String, Integer> 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;
}
}