In the previous post “Start activity using intent for specified MIME type”, the app will quit whenever you press the BACK key on list of files in any directory.
Now in this example with the help of stack and onBackPressed() function history can be added on back pressed.
Algorithm:
1.) Create a new project by File-> New -> Android Project name it FileExplorerHistory.
2.) Write following permissions into android manifest.xml:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
3.) You must have some directories and some files in those directories on your storage media to check the correct output.
4.) Run for output.
Steps:
1.) Create a project named FileExplorerHistory and set the information as stated in the image.
Build Target: Android 4.4
Application Name: FileExplorerHistory
Package Name: com.example. FileExplorerHistory
Activity Name: FileExplorerHistoryActivity
2.) Open FileExplorerHistoryActivity.java file and write following code there:
package com.example.fileexplorerhistory; import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Stack; import android.app.ListActivity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.webkit.MimeTypeMap; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Toast; public class FileExplorerHistoryActivity extends ListActivity { private List<String> fileList = new ArrayList<String>(); Stack<File> history; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); history = new Stack<File>(); File root = new File(Environment .getExternalStorageDirectory() .getAbsolutePath()); ListDir(root); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub File selected = new File(fileList.get(position)); if(selected.isDirectory()){ ListDir(selected); }else { Uri selectedUri = Uri.fromFile(selected); String fileExtension = MimeTypeMap.getFileExtensionFromUrl(selectedUri.toString()); String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension); Toast.makeText(FileExplorerHistoryActivity.this, "FileExtension: " + fileExtension + "n" + "MimeType: " + mimeType, Toast.LENGTH_LONG).show(); //Start Activity to view the selected file Intent intent; intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(selectedUri, mimeType); startActivity(intent); } } void ListDir(File f){ history.push(f); File[] files = f.listFiles(); fileList.clear(); for (File file : files){ fileList.add(file.getPath()); } ArrayAdapter<String> directoryList = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, fileList); setListAdapter(directoryList); } @Override public void onBackPressed() { // TODO Auto-generated method stub history.pop(); //remove current directory if(history.empty()){ super.onBackPressed(); }else{ File selected = history.pop(); //Get the File on Top of history ListDir(selected); } } }
3.) Compile and build the project.
Output
Top 10 Android App Development Trends | 2020 Guide
The 9 Best Programming Languages To Learn In 2020
10 Best Programming Languages to Learn in 2019 (for Job & Future)
9 Popular Cross-Platform Tools for App Development in 2019
10 Reasons to Learn Python Programming Language in 2019
Top Mobile App Development Trends of the Year 2019
20 Best iOS App Development Tutorials and Online Learning Resources
Top 15 Best Android Apps For C Programming | 2018 Exclusive