/
MainActivity.java
180 lines (154 loc) · 7.5 KB
/
MainActivity.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
package com.androidcodeman.simpleimagegallery;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import com.androidcodeman.simpleimagegallery.utils.MarginDecoration;
import com.androidcodeman.simpleimagegallery.utils.PicHolder;
import com.androidcodeman.simpleimagegallery.utils.imageFolder;
import com.androidcodeman.simpleimagegallery.utils.itemClickListener;
import com.androidcodeman.simpleimagegallery.utils.pictureFacer;
import com.androidcodeman.simpleimagegallery.utils.pictureFolderAdapter;
import java.util.ArrayList;
/**
* Author CodeBoy722
*
* The main Activity start and loads all folders containing images in a RecyclerView
* this folders are gotten from the MediaStore by the Method getPicturePaths()
*/
public class MainActivity extends AppCompatActivity implements itemClickListener {
RecyclerView folderRecycler;
TextView empty;
private static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1;
/**
* Request the user for permission to access media files and read images on the device
* this will be useful as from api 21 and above, if this check is not done the Activity will crash
*
* Setting up the RecyclerView and getting all folders that contain pictures from the device
* the getPicturePaths() returns an ArrayList of imageFolder objects that is then used to
* create a RecyclerView Adapter that is set to the RecyclerView
*
* @param savedInstanceState saving the activity state
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
//____________________________________________________________________________________
empty =findViewById(R.id.empty);
folderRecycler = findViewById(R.id.folderRecycler);
folderRecycler.addItemDecoration(new MarginDecoration(this));
folderRecycler.hasFixedSize();
ArrayList<imageFolder> folds = getPicturePaths();
if(folds.isEmpty()){
empty.setVisibility(View.VISIBLE);
}else{
RecyclerView.Adapter folderAdapter = new pictureFolderAdapter(folds,MainActivity.this,this);
folderRecycler.setAdapter(folderAdapter);
}
changeStatusBarColor();
}
/**1
* @return
* gets all folders with pictures on the device and loads each of them in a custom object imageFolder
* the returns an ArrayList of these custom objects
*/
private ArrayList<imageFolder> getPicturePaths(){
ArrayList<imageFolder> picFolders = new ArrayList<>();
ArrayList<String> picPaths = new ArrayList<>();
Uri allImagesuri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = { MediaStore.Images.ImageColumns.DATA ,MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,MediaStore.Images.Media.BUCKET_ID};
Cursor cursor = this.getContentResolver().query(allImagesuri, projection, null, null, null);
try {
if (cursor != null) {
cursor.moveToFirst();
}
do{
imageFolder folds = new imageFolder();
String name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME));
String folder = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME));
String datapath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
//String folderpaths = datapath.replace(name,"");
String folderpaths = datapath.substring(0, datapath.lastIndexOf(folder+"/"));
folderpaths = folderpaths+folder+"/";
if (!picPaths.contains(folderpaths)) {
picPaths.add(folderpaths);
folds.setPath(folderpaths);
folds.setFolderName(folder);
folds.setFirstPic(datapath);//if the folder has only one picture this line helps to set it as first so as to avoid blank image in itemview
folds.addpics();
picFolders.add(folds);
}else{
for(int i = 0;i<picFolders.size();i++){
if(picFolders.get(i).getPath().equals(folderpaths)){
picFolders.get(i).setFirstPic(datapath);
picFolders.get(i).addpics();
}
}
}
}while(cursor.moveToNext());
cursor.close();
} catch (Exception e) {
e.printStackTrace();
}
for(int i = 0;i < picFolders.size();i++){
Log.d("picture folders",picFolders.get(i).getFolderName()+" and path = "+picFolders.get(i).getPath()+" "+picFolders.get(i).getNumberOfPics());
}
//reverse order ArrayList
/* ArrayList<imageFolder> reverseFolders = new ArrayList<>();
for(int i = picFolders.size()-1;i > reverseFolders.size()-1;i--){
reverseFolders.add(picFolders.get(i));
}*/
return picFolders;
}
@Override
public void onPicClicked(PicHolder holder, int position, ArrayList<pictureFacer> pics) {
}
/**
* Each time an item in the RecyclerView is clicked this method from the implementation of the transitListerner
* in this activity is executed, this is possible because this class is passed as a parameter in the creation
* of the RecyclerView's Adapter, see the adapter class to understand better what is happening here
* @param pictureFolderPath a String corresponding to a folder path on the device external storage
*/
@Override
public void onPicClicked(String pictureFolderPath,String folderName) {
Intent move = new Intent(MainActivity.this,ImageDisplay.class);
move.putExtra("folderPath",pictureFolderPath);
move.putExtra("folderName",folderName);
//move.putExtra("recyclerItemSize",getCardsOptimalWidth(4));
startActivity(move);
}
/**
* Default status bar height 24dp,with code API level 24
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void changeStatusBarColor()
{
Window window = this.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(ContextCompat.getColor(getApplicationContext(),R.color.black));
}
}