From 0ab046c8d5dd674ae1f3fec9a977ed2e24c658ee Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 20 Feb 2016 21:06:22 +0100 Subject: [PATCH] display a grid of photos on a directory selection --- .../simplemobiletools/com/MainActivity.java | 8 +-- .../simplemobiletools/com/PhotosActivity.java | 38 ++++++++++- .../simplemobiletools/com/PhotosAdapter.java | 64 +++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 24 +++---- app/src/main/res/layout/activity_photos.xml | 12 ++-- app/src/main/res/layout/directory_item.xml | 7 +- app/src/main/res/layout/photo_item.xml | 13 ++++ app/src/main/res/values/colors.xml | 1 + app/src/main/res/values/dimens.xml | 3 +- 9 files changed, 142 insertions(+), 28 deletions(-) create mode 100644 app/src/main/java/gallery/simplemobiletools/com/PhotosAdapter.java create mode 100644 app/src/main/res/layout/photo_item.xml diff --git a/app/src/main/java/gallery/simplemobiletools/com/MainActivity.java b/app/src/main/java/gallery/simplemobiletools/com/MainActivity.java index 1bfc1ceac..219cf6f79 100644 --- a/app/src/main/java/gallery/simplemobiletools/com/MainActivity.java +++ b/app/src/main/java/gallery/simplemobiletools/com/MainActivity.java @@ -24,7 +24,7 @@ public class MainActivity extends AppCompatActivity implements OnItemClickListen protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - final GridView gridView = (GridView) findViewById(R.id.photo_grid); + final GridView gridView = (GridView) findViewById(R.id.directories_grid); dirs = new ArrayList<>(getDirectories().values()); final DirectoryAdapter adapter = new DirectoryAdapter(this, dirs); @@ -41,7 +41,8 @@ public class MainActivity extends AppCompatActivity implements OnItemClickListen if (cursor != null && cursor.moveToFirst()) { final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); do { - final File file = new File(cursor.getString(pathIndex)); + final String path = cursor.getString(pathIndex); + final File file = new File(path); final String fileDir = file.getParent().toLowerCase(); if (directories.containsKey(fileDir)) { @@ -49,9 +50,8 @@ public class MainActivity extends AppCompatActivity implements OnItemClickListen final int newImageCnt = directory.getPhotoCnt() + 1; directory.setPhotoCnt(newImageCnt); } else { - final String thumbnail = file.getAbsolutePath(); final String dirName = fileDir.substring(fileDir.lastIndexOf("/") + 1); - directories.put(fileDir, new Directory(fileDir, thumbnail, dirName, 1)); + directories.put(fileDir, new Directory(fileDir, path, dirName, 1)); } } while (cursor.moveToNext()); cursor.close(); diff --git a/app/src/main/java/gallery/simplemobiletools/com/PhotosActivity.java b/app/src/main/java/gallery/simplemobiletools/com/PhotosActivity.java index 3444c2781..2ab280ac7 100644 --- a/app/src/main/java/gallery/simplemobiletools/com/PhotosActivity.java +++ b/app/src/main/java/gallery/simplemobiletools/com/PhotosActivity.java @@ -1,15 +1,51 @@ package gallery.simplemobiletools.com; +import android.database.Cursor; +import android.net.Uri; import android.os.Bundle; +import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; +import android.view.View; +import android.widget.AdapterView; +import android.widget.GridView; -public class PhotosActivity extends AppCompatActivity { +import java.util.ArrayList; +import java.util.List; + +public class PhotosActivity extends AppCompatActivity implements AdapterView.OnItemClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_photos); + final GridView gridView = (GridView) findViewById(R.id.photos_grid); + final PhotosAdapter adapter = new PhotosAdapter(this, getPhotos()); + gridView.setAdapter(adapter); + gridView.setOnItemClickListener(this); + } + + private List getPhotos() { + final List photos = new ArrayList<>(); final String path = getIntent().getStringExtra(Constants.DIRECTORY); + final Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; + final String where = MediaStore.Images.Media.DATA + " like ? "; + final String[] args = new String[]{path + "%"}; + final String[] columns = {MediaStore.Images.Media.DATA}; + final Cursor cursor = getContentResolver().query(uri, columns, where, args, null); + + if (cursor != null && cursor.moveToFirst()) { + final int pathIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); + do { + photos.add(cursor.getString(pathIndex)); + } while (cursor.moveToNext()); + cursor.close(); + } + return photos; + } + + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + } } diff --git a/app/src/main/java/gallery/simplemobiletools/com/PhotosAdapter.java b/app/src/main/java/gallery/simplemobiletools/com/PhotosAdapter.java new file mode 100644 index 000000000..2572a1109 --- /dev/null +++ b/app/src/main/java/gallery/simplemobiletools/com/PhotosAdapter.java @@ -0,0 +1,64 @@ +package gallery.simplemobiletools.com; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.ImageView; + +import com.bumptech.glide.Glide; + +import java.util.List; + +public class PhotosAdapter extends BaseAdapter { + private final Context context; + private final List photos; + private final LayoutInflater inflater; + + public PhotosAdapter(Context context, List photos) { + this.context = context; + this.photos = photos; + this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + } + + @Override + public View getView(int position, View view, ViewGroup parent) { + ViewHolder holder; + if (view == null) { + view = inflater.inflate(R.layout.photo_item, parent, false); + holder = new ViewHolder(view); + view.setTag(holder); + } else { + holder = (ViewHolder) view.getTag(); + } + + String path = photos.get(position); + Glide.with(context).load(path).centerCrop().crossFade().into(holder.photoThumbnail); + + return view; + } + + @Override + public int getCount() { + return photos.size(); + } + + @Override + public Object getItem(int position) { + return photos.get(position); + } + + @Override + public long getItemId(int position) { + return 0; + } + + static class ViewHolder { + ImageView photoThumbnail; + + public ViewHolder(View view) { + photoThumbnail = (ImageView) view.findViewById(R.id.photo_thumbnail); + } + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index e58c80115..2867ef856 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,18 +1,12 @@ - - - - - + android:layout_height="wrap_content" + android:background="@android:color/black" + android:columnWidth="@dimen/dir_tmb_size" + android:horizontalSpacing="1dp" + android:numColumns="auto_fit" + android:stretchMode="columnWidth" + android:verticalSpacing="1dp"/> diff --git a/app/src/main/res/layout/activity_photos.xml b/app/src/main/res/layout/activity_photos.xml index c881b43c0..7b47eb155 100644 --- a/app/src/main/res/layout/activity_photos.xml +++ b/app/src/main/res/layout/activity_photos.xml @@ -1,8 +1,12 @@ - - - + android:background="@android:color/black" + android:columnWidth="@dimen/photo_tmb_size" + android:horizontalSpacing="1dp" + android:numColumns="auto_fit" + android:stretchMode="columnWidth" + android:verticalSpacing="1dp"/> diff --git a/app/src/main/res/layout/directory_item.xml b/app/src/main/res/layout/directory_item.xml index c522a2ce9..0c65fd489 100644 --- a/app/src/main/res/layout/directory_item.xml +++ b/app/src/main/res/layout/directory_item.xml @@ -2,13 +2,13 @@ + android:layout_height="match_parent" + android:background="@color/tmb_background"> + android:layout_height="match_parent"/> + diff --git a/app/src/main/res/layout/photo_item.xml b/app/src/main/res/layout/photo_item.xml new file mode 100644 index 000000000..fc8a2ce07 --- /dev/null +++ b/app/src/main/res/layout/photo_item.xml @@ -0,0 +1,13 @@ + + + + + + diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 3ab3e9cbc..422169a59 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -3,4 +3,5 @@ #3F51B5 #303F9F #FF4081 + #ff222222 diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index 846ddefce..6e37ff7e0 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -1,4 +1,5 @@ 16dp - 150dp + 150dp + 100dp