How To Make File Manager In Android studio | Simple File Manager Source Code For Android Studio
Hello everyone, are you new to Android studio and want to learn how to make a simple file manager in Android studio. Then you are at right place. In this tutorial we are going to teach you How To Create File Manager In Android Studio
Before we start, let’s clear something. We are assuming you are not absolute beginner of andr studio. We are assuming you have atleast small knowledge of what is a activity, what are ListView, ImageView and more basic things in Android atudio. One more thing, this tutorial is going to be in Java language. Currently we are not providing in Kotlin, But in future we will update. Now lets start.
First Of All You need to Create an empty project in Android Studio Or AIDE. Now in java folder create These classes:
– MainActivity.Java ( Created Default by Android studio)
– FileList.java
Now in the res/layout folder create these two files
– activity_main.xml ( Created default by Android studio)
– file_list.xml
– file_list_item.xml
Keep this thing in mind that do not write file name in layout folder in capital letters. It will cause error in Android studio.
Now in AndroidManifest.xml set permission to read and write external storage like below.
<uses-permission android:name="android.permission.Read_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Now in activity_main.xml Create a button and set onClick=”start”. Or Simply paste this below codes in activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"><Button
android:text="Start App"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/mainButton1"
android:onClick="start"/></LinearLayout>
Now open MainActivity.java and make a function name as “start” because we set onClick=”start”.
MainActivity.java
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
private void start(){
}
}
Now in start method, first check if permissions are granted. If permissions are not granted, we will ask for permissions otherwise we start next activity.
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
private void start(View v){
if(checkPermission() == false){
requestPermission();
} else{
Intent i = new Intent(MainActivity.this,FileList.class);
startActivity(i);
}
}public boolean checkPermission() {
int result = ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE);
int result1 = ContextCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.READ_EXTERNAL_STORAGE);
return result == PackageManager.PERMISSION_GRANTED &&
result1 == PackageManager.PERMISSION_GRANTED;
}private void requestPermission() {
ActivityCompat.requestPermissions(Start.this, new
String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
}}
Now again open The AndroidManifiest.xml file and add this line inside application tag
<activity
android:name=”.FileList”
android:label=”@string/app_name” > </activity>
Now We have to setup the FileList activity. In this activity we will list the directories and files. But First Open the file_list.xml file and create a listview.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"><ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/idListView"/></LinearLayout>
Open file_list_item.xml and paste this code
file_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp"><ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:id="@+id/idFileListItemImageViewIcon"/><TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:id="@+id/idFileListItemTextViewTitle"
android:layout_toRightOf="@id/idFileListItemImageViewIcon"
android:layout_marginLeft="10dp"/></RelativeLayout>
Now open FileList.java file and paste these codes. In FileList.java, we make a list of files and folder. We will make a array of files and folders and set it to a ListView. Now we will add listners on items of listview so that when user click on ListView item, they will open file or folder. Also We will create a custom adaptor which is going to bind with ListView.
FileList.java
import android.app.*;
import android.os.*;
import android.widget.*;
import java.io.*;
import java.util.*;
import android.view.*;
import android.content.*;public class FileList extends Activity
{
ListView listView;
ArrayList<String> filesArray= new ArrayList<String>();
Context context;
String dir;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO: Implement this method
super.onCreate(savedInstanceState);
setContentView(R.layout.file_list);
context = this;
if(getIntent().hasExtra("path")){
dir = getIntent().getExtras().getString("path");
}else{
dir = Environment.getExternalStorageDirectory()+"/";
}
listView = findViewById(R.id.idListView);
File files = new File(dir);
File[] listFolders = files.listFiles(new FileFilter(){@Override
public boolean accept(File file)
{
// TODO: Implement this method
return file.isDirectory();
}});
for(int i=0; i<listFolders.length; i++){
filesArray.add(listFolders[i].getAbsolutePath());
}
File[] listFiles = files.listFiles(new FileFilter(){@Override
public boolean accept(File file)
{
// TODO: Implement this method
return file.isFile();
}
});
for(int i=0; i<listFiles.length; i++){
filesArray.add(listFiles[i].getAbsolutePath());
}
MyAdapter adapter = new MyAdapter(context,filesArray);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){@Override
public void onItemClick(AdapterView<?> p1, View p2, int position, long p4)
{
File mFile = new File(filesArray.get(position));
if(mFile.isFile()){
Toast.makeText(context,"Handle file here",Toast.LENGTH_SHORT).show();
}else{
Intent i = new Intent(context,FileList.class);
i.putExtra("path",mFile.getAbsolutePath());
startActivity(i);
}
}
});
}
class MyAdapter extends ArrayAdapter {
Context context;
ArrayList<String> paths;public MyAdapter(Context context,ArrayList<String> paths) {
super(context, R.layout.file_list_item,R.id.idFileListItemTextViewTitle,paths);this.paths=paths;
this.context = context;}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//Inflating the layout
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.file_list_item,parent,false);//Get the reference to the view objects
TextView myTitle = (TextView) row.findViewById(R.id.idFileListItemTextViewTitle);
ImageView icon = row.findViewById(R.id.idFileListItemImageViewIcon);
final String mPath = paths.get(position);String mName = new File(mPath).getName();
myTitle.setText(mName);
finalFile file = new File(paths.get(position));
if(file.isDirectory()){
icon.setBackgroundResource(R.drawable.ic_folder);
} else{
icon.setBackgroundResource(R.drawable.ic_file);
}return row;
}}
}
This is a basic example of File manager but this is not complete. But further process is not so hard. You have to create FileViewer script which will decide what type of file it is and will transfer user to next activity. You can make activities like ImageView, MusicPlayer, VideoPlayer and TextViewer. Also You can set file editing system like rename or delete. If you want full tutorial then please comment below. We will provide you full code as soon as possible.