33
44import android .os .Bundle ;
55import android .os .Environment ;
6+ import android .view .LayoutInflater ;
7+ import android .view .View ;
8+ import android .view .ViewGroup ;
9+
610import androidx .fragment .app .Fragment ;
711import androidx .recyclerview .widget .DividerItemDecoration ;
812import androidx .recyclerview .widget .LinearLayoutManager ;
913import androidx .recyclerview .widget .RecyclerView ;
10- import android .view .LayoutInflater ;
11- import android .view .View ;
12- import android .view .ViewGroup ;
1314
1415import java .io .File ;
1516import java .util .ArrayList ;
1617import java .util .List ;
1718
1819
19-
2020public class SimpleFileExplorerFragment extends Fragment implements AdapterListener {
2121
2222 private RecyclerView recyclerView ;
@@ -41,7 +41,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
4141 return view ;
4242 }
4343
44- private void initRecyclerView (View view ){
44+ private void initRecyclerView (View view ) {
4545 this .recyclerView = view .findViewById (R .id .recycler_file_explorer );
4646 this .fileExplorerAdapter = new SimpleFileExplorerAdapter (getContext ());
4747 this .fileExplorerAdapter .setAdapterListener (this );
@@ -51,41 +51,44 @@ private void initRecyclerView(View view){
5151 this .recyclerView .addItemDecoration (new DividerItemDecoration (this .recyclerView .getContext (), DividerItemDecoration .VERTICAL ));
5252 }
5353
54- private void loadDirectory (){
54+ private void loadDirectory () {
5555 File root = Environment .getExternalStorageDirectory ();
5656
57- if (this .selectedAbsolutePath != null ){
57+ if (this .selectedAbsolutePath != null ) {
5858 root = new File (this .selectedAbsolutePath );
59- }
60- else {
59+ } else {
6160 this .selectedAbsolutePath = root .getAbsolutePath ();
6261 }
6362
6463 List <FileModel > fileModelList = new ArrayList <>();
6564
66- if (root .isDirectory ()){
67- for (File file : root .listFiles ()) {
68- if (file .isDirectory ()){
65+
66+ final File [] listFiles = root .listFiles ();
67+ if (root .isDirectory () && listFiles != null ) {
68+ for (File file : listFiles ) {
69+ if (file .isDirectory ()) {
6970 fileModelList .add (new FileModel (file .getAbsolutePath (), FileModelType .DIRECTORY ));
70- }
71- else {
72- fileModelList .add (new FileModel (file .getAbsolutePath (), file .getParentFile ().getAbsolutePath (), FileModelType .FILE ));
71+ } else {
72+ final File parent = file .getParentFile ();
73+ if (parent == null ) {
74+ continue ;
75+ }
76+ fileModelList .add (new FileModel (file .getAbsolutePath (), parent .getAbsolutePath (), FileModelType .FILE ));
7377 }
7478 }
75- }
76- else {
79+ } else {
7780 fileModelList .add (new FileModel (root .getAbsolutePath (), FileModelType .DIRECTORY ));
7881 }
7982
8083 this .updateRecyclerList (fileModelList );
8184 }
8285
83- private void updateRecyclerList (List <FileModel > fileModels ){
86+ private void updateRecyclerList (List <FileModel > fileModels ) {
8487 this .fileExplorerAdapter .loadDirectory (fileModels );
8588 }
8689
8790
88- private void initViews (View view ){
91+ private void initViews (View view ) {
8992 this .initRecyclerView (view );
9093 }
9194
@@ -99,11 +102,11 @@ public void onFileClick(FileModel fileModel) {
99102 this .activityListener .onFileSelect (fileModel );
100103 }
101104
102- void setListeners (ActivityListener activityListener ){
105+ void setListeners (ActivityListener activityListener ) {
103106 this .activityListener = activityListener ;
104107 }
105108
106- void setDirectory (String dir ){
109+ void setDirectory (String dir ) {
107110 this .selectedAbsolutePath = dir ;
108111 }
109112
0 commit comments