Created
September 12, 2018 19:59
-
-
Save Binary-Finery/398198150a2d39f8608800cd050a0d30 to your computer and use it in GitHub Desktop.
explore external storage and list all .txt files in all directories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.os.Bundle; | |
import android.os.Environment; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.Toolbar; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import java.io.File; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Stack; | |
public class FileExplorer extends AppCompatActivity { | |
private TextView tvPath; | |
private ListView listView; | |
private List<FileData> allFiles; | |
private List<FileData> dirFiles; | |
private Stack<File> fileStack; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_file_explorer); | |
Toolbar toolbar = findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
initViews(); | |
fileStack = new Stack<>(); | |
fileStack.push(new File(Environment.getExternalStorageDirectory(), "")); | |
dirFiles = new ArrayList<>(); | |
allFiles = getDirFiles(); | |
FileAdapter adapter = new FileAdapter(this, allFiles); | |
listView.setAdapter(adapter); | |
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { | |
String fName = allFiles.get(i).getfName(); | |
File file = new File(fileStack.peek(), fName); | |
if (file.isFile()) { | |
Toast.makeText(getApplicationContext(), "this is a txt file", Toast.LENGTH_LONG).show(); | |
}else { | |
fileStack.push(new File(fileStack.peek(), fName)); | |
populateListView(); | |
} | |
} | |
}); | |
} | |
private List<FileData> getDirFiles() { | |
dirFiles.clear(); | |
File dir = new File(fileStack.peek(), ""); | |
for (File file : dir.listFiles()) { | |
if ((file.isFile() && file.getName().endsWith(".txt")) || file.isDirectory()) { | |
dirFiles.add(new FileData(file.getName(), file.isDirectory())); | |
} | |
} | |
tvPath.setText(dir.toString()); | |
return dirFiles; | |
} | |
private void populateListView() { | |
allFiles = getDirFiles(); | |
listView.setAdapter(new FileAdapter(FileExplorer.this, allFiles)); | |
} | |
public void clickEvent(View view) { | |
switch (view.getId()) { | |
case R.id.iv_prev_dir: | |
if (fileStack.size() > 1) { | |
fileStack.pop(); | |
populateListView(); | |
} | |
break; | |
/* handle additional click events */ | |
} | |
} | |
private void initViews() { | |
listView = findViewById(R.id.lv_files); | |
tvPath = findViewById(R.id.tv_path); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment