-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlbumActivity.java
213 lines (177 loc) · 6.82 KB
/
AlbumActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package com.github.windsekirun.naraepicker;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.gc.materialdesign.views.ProgressBarCircularIndeterminate;
import java.io.File;
import java.util.ArrayList;
import java.util.HashSet;
/**
* NaraePicker
* class: AlbumFragment
* Created by WindSekirun on 2015. 7. 17..
*/
public class AlbumActivity extends AppCompatActivity {
Toolbar toolbar;
AlbumGridAdapter adapter;
ArrayList<Album> itemSet = new ArrayList<>();
EmptyRecyclerView list;
ProgressBarCircularIndeterminate progressBar;
GridLayoutManager mLayoutManager;
int limit;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select);
toolbar = (Toolbar) findViewById(R.id.toolbar);
list = (EmptyRecyclerView) findViewById(R.id.list);
progressBar = (ProgressBarCircularIndeterminate) findViewById(R.id.progressBarCircularIndeterminate);
list.setEmptyView(progressBar);
list.setHasFixedSize(true);
adapter = new AlbumGridAdapter(itemSet);
mLayoutManager = new GridLayoutManager(this, 2);
list.setLayoutManager(mLayoutManager);
list.setAdapter(adapter);
new LoadGalleryList().execute();
toolbarInflate();
limit = getIntent().getIntExtra("limit", 4);
}
public void toolbarInflate() {
toolbar.setBackgroundColor(Material.getMaterialBlueColor(500));
toolbar.setTitleTextColor(0xffffffff);
toolbar.setTitle("사진을 선택해주세요.");
setSupportActionBar(toolbar);
}
public class LoadGalleryList extends AsyncTask<Void, Void, Void> {
ArrayList<Album> tempSet = new ArrayList<>();
@Override
protected Void doInBackground(Void... params) {
HashSet<String> albumSet = new HashSet<>();
File file;
Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Images.Media.BUCKET_DISPLAY_NAME, MediaStore.Images.Media.DATA}, null, null, MediaStore.Images.Media.DATE_ADDED);
if (cursor.moveToLast()) {
do {
String album = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME));
String image = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
file = new File(image);
if (file.exists() && !albumSet.contains(album)) {
tempSet.add(new Album(album, image));
albumSet.add(album);
}
} while (cursor.moveToPrevious());
}
cursor.close();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
itemSet.addAll(0, tempSet);
adapter.notifyDataSetChanged();
tempSet.clear();
tempSet.trimToSize();
}
}
public class ItemClickListener implements View.OnClickListener {
int position;
public ItemClickListener(int i) {
position = i;
}
@Override
public void onClick(View v) {
Album album = itemSet.get(position);
Intent imageIntent = new Intent(AlbumActivity.this, ImageActivity.class);
imageIntent.putExtra("folderName", album.name);
imageIntent.putExtra("limit", limit);
imageIntent.setFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(imageIntent);
finish();
}
}
public class AlbumGridAdapter extends RecyclerView.Adapter<AlbumItem> {
ArrayList<Album> dataSet;
Context c;
public AlbumGridAdapter(ArrayList<Album> itemSet) {
dataSet = itemSet;
}
@Override
public AlbumItem onCreateViewHolder(ViewGroup parent, int viewType) {
c = parent.getContext();
View v = LayoutInflater.from(c).inflate(R.layout.row_folder, parent, false);
return new AlbumItem(v);
}
@Override
public void onBindViewHolder(AlbumItem holder, int position) {
holder.itemView.setOnClickListener(new ItemClickListener(position));
Album data = dataSet.get(position);
holder.name.setText(data.name);
Glide.with(c).load(data.imagePath).thumbnail(0.5f).into(holder.thumbnail);
holder.name.setTextColor(0xffffffff);
holder.nameRoot.setBackgroundColor(Material.getMaterialBlueColor(700));
}
@Override
public int getItemCount() {
return dataSet.size();
}
}
public class Album implements Parcelable {
public String name;
public String imagePath;
public Album(String name, String imagePath) {
this.name = name;
this.imagePath = imagePath;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(imagePath);
}
public final Creator<Album> CREATOR = new Creator<Album>() {
@Override
public Album createFromParcel(Parcel source) {
return new Album(source);
}
@Override
public Album[] newArray(int size) {
return new Album[size];
}
};
private Album(Parcel in) {
name = in.readString();
imagePath = in.readString();
}
}
public class AlbumItem extends RecyclerView.ViewHolder {
public ImageView thumbnail;
public TextView name;
public RelativeLayout nameRoot;
public AlbumItem(View itemView) {
super(itemView);
thumbnail = (ImageView) itemView.findViewById(R.id.img_thumbnail);
name = (TextView) itemView.findViewById(R.id.img_text);
nameRoot = (RelativeLayout) itemView.findViewById(R.id.img_text_root);
}
}
}