{"id":242,"date":"2013-12-30T16:36:26","date_gmt":"2013-12-30T22:36:26","guid":{"rendered":"http:\/\/www.scorchworks.com\/Blog\/?p=242"},"modified":"2024-05-01T20:11:02","modified_gmt":"2024-05-02T01:11:02","slug":"simple-file-dialog-for-android-applications","status":"publish","type":"post","link":"https:\/\/www.scorchworks.com\/Blog\/simple-file-dialog-for-android-applications\/","title":{"rendered":"Simple File Dialog For Android Applications"},"content":{"rendered":"<p>Since there isn&#8217;t a built in file dialog interface for android development I have been searching for one that I could use without starting from scratch. I did eventually find a <a href=\"http:\/\/www.codeproject.com\/Articles\/547636\/Android-Ready-to-use-simple-directory-chooser-dial\">Simple Directory Chooser<\/a> with a very lenient license <a href=\"http:\/\/www.codeproject.com\/info\/cpol10.aspx\">(The Code Project Open License (CPOL) 1.02).<\/a> The directory chooser I found uses a dialog interface so there is no need to create an additional activity to use it.<\/p>\n<p>The directory chooser was great but I needed a file dialog for choosing files for opening and saving not a directory chooser. To get the file dialog I needed I modified the original directory chooser adding to it as needed to create a Simple File Dialog for my own use. I thought I would post my modified version here for others to use or build on.<\/p>\n<p>The file dialog code consists of a single file and uses standard Android resources. This file dialog code should be very portable and easily inserted into any project to add menu driven file selection within Android.\u00a0 Screen shots of the three file dialog modes (Open, Save, and Folder Select) are shown below. The &#8220;Save&#8221; and &#8220;Folder Select&#8221; modes include a button for adding a new folder to the file structure.<\/p>\n<p style=\"text-align: center;\"><a href=\"http:\/\/www.scorchworks.com\/Blog\/wp-content\/uploads\/open.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-279\" alt=\"Open Dialog\" src=\"http:\/\/www.scorchworks.com\/Blog\/wp-content\/uploads\/open-180x300.png\" width=\"180\" height=\"300\" srcset=\"https:\/\/www.scorchworks.com\/Blog\/wp-content\/uploads\/open-180x300.png 180w, https:\/\/www.scorchworks.com\/Blog\/wp-content\/uploads\/open.png 330w\" sizes=\"(max-width: 180px) 100vw, 180px\" \/><\/a><a href=\"http:\/\/www.scorchworks.com\/Blog\/wp-content\/uploads\/save.png\"> <img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-276\" alt=\"save\" src=\"http:\/\/www.scorchworks.com\/Blog\/wp-content\/uploads\/save-180x300.png\" width=\"180\" height=\"300\" srcset=\"https:\/\/www.scorchworks.com\/Blog\/wp-content\/uploads\/save-180x300.png 180w, https:\/\/www.scorchworks.com\/Blog\/wp-content\/uploads\/save.png 330w\" sizes=\"(max-width: 180px) 100vw, 180px\" \/><\/a><a href=\"http:\/\/www.scorchworks.com\/Blog\/wp-content\/uploads\/folder.png\"> <img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-277\" alt=\"folder\" src=\"http:\/\/www.scorchworks.com\/Blog\/wp-content\/uploads\/folder-180x300.png\" width=\"180\" height=\"300\" srcset=\"https:\/\/www.scorchworks.com\/Blog\/wp-content\/uploads\/folder-180x300.png 180w, https:\/\/www.scorchworks.com\/Blog\/wp-content\/uploads\/folder.png 330w\" sizes=\"(max-width: 180px) 100vw, 180px\" \/><\/a><\/p>\n<p style=\"text-align: left;\">The code can be downloaded here: <a href=\"http:\/\/www.scorchworks.com\/MISC\/SimpleFileDialog.java\">SimpleFileDialog.java<\/a> and <a href=\"http:\/\/www.scorchworks.com\/MISC\/MainActivity.java\">MainActivity.java<\/a><\/p>\n<h1>Bare Bones Application Using the Simple File Dialog<\/h1>\n<p>This is a very bare bones application with three buttons that call the Simple File Dialog in all three modes (Save, Open and Folder Select). To make this application I just started a new Android Application Project in Eclipse. Edited the Layout to add three buttons and edited the MainActivity.java file. The finished MainActivity.java file is shown below.<\/p>\n<p><a href=\"http:\/\/www.scorchworks.com\/Blog\/wp-content\/uploads\/main_screen.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-278\" alt=\"Demo Screen\" src=\"http:\/\/www.scorchworks.com\/Blog\/wp-content\/uploads\/main_screen-180x300.png\" width=\"180\" height=\"300\" srcset=\"https:\/\/www.scorchworks.com\/Blog\/wp-content\/uploads\/main_screen-180x300.png 180w, https:\/\/www.scorchworks.com\/Blog\/wp-content\/uploads\/main_screen.png 330w\" sizes=\"(max-width: 180px) 100vw, 180px\" \/><\/a><br \/>\n<!-------------------------------- BEGIN CODE ---------------------------------><\/p>\n<pre style=\"width: 94%; border: black 2px solid; color: black; background-color: #d0d0d0;\">\/\/MainActivity.java\r\npackage com.scorchworks.demo;\r\n\r\nimport android.os.Bundle;\r\nimport android.app.Activity;\r\nimport android.view.Menu;\r\nimport android.view.View;\r\n\r\nimport android.widget.Button;\r\nimport android.view.View.OnClickListener;\r\nimport android.widget.Toast;\r\n\r\n\/\/import android.view.View;\r\n\r\npublic class MainActivity extends Activity {\r\n\r\n\t@Override\r\n\tprotected void onCreate(Bundle savedInstanceState) {\r\n\t\tsuper.onCreate(savedInstanceState);\r\n\t\tsetContentView(R.layout.activity_main);\r\n\r\n\t\t\/\/Button1\r\n\t\tButton dirChooserButton1 = (Button) findViewById(R.id.button1);\r\n\t\tdirChooserButton1.setOnClickListener(new OnClickListener() \r\n\t\t{\r\n\t\t\tString m_chosen;\r\n\t\t\t@Override\r\n\t\t\tpublic void onClick(View v) {\r\n\t\t\t\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\t\t\t\t\/\/Create FileOpenDialog and register a callback\r\n\t\t\t\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\t\t\t\tSimpleFileDialog FileOpenDialog =  new SimpleFileDialog(MainActivity.this, \"FileOpen\",\r\n\t\t\t\t\t\tnew SimpleFileDialog.SimpleFileDialogListener()\r\n\t\t\t\t{\r\n\t\t\t\t\t@Override\r\n\t\t\t\t\tpublic void onChosenDir(String chosenDir) \r\n\t\t\t\t\t{\r\n\t\t\t\t\t\t\/\/ The code in this function will be executed when the dialog OK button is pushed \r\n\t\t\t\t\t\tm_chosen = chosenDir;\r\n\t\t\t\t\t\tToast.makeText(MainActivity.this, \"Chosen FileOpenDialog File: \" + \r\n\t\t\t\t\t\t\t\tm_chosen, Toast.LENGTH_LONG).show();\r\n\t\t\t\t\t}\r\n\t\t\t\t});\r\n\r\n\t\t\t\t\/\/You can change the default filename using the public variable \"Default_File_Name\"\r\n\t\t\t\tFileOpenDialog.Default_File_Name = \"\";\r\n\t\t\t\tFileOpenDialog.chooseFile_or_Dir();\r\n\r\n\t\t\t\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\r\n\t\t\t}\r\n\t\t});\r\n\r\n\t\t\/\/Button2\r\n\t\tButton dirChooserButton2 = (Button) findViewById(R.id.button2);\r\n\t\tdirChooserButton2.setOnClickListener(new OnClickListener() \r\n\t\t{\r\n\t\t\tString m_chosen;\r\n\t\t\t@Override\r\n\t\t\tpublic void onClick(View v) {\r\n\t\t\t\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\t\t\t\t\/\/Create FileSaveDialog and register a callback\r\n\t\t\t\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\t\t\t\tSimpleFileDialog FileSaveDialog =  new SimpleFileDialog(MainActivity.this, \"FileSave\",\r\n\t\t\t\t\t\tnew SimpleFileDialog.SimpleFileDialogListener()\r\n\t\t\t\t{\r\n\t\t\t\t\t@Override\r\n\t\t\t\t\tpublic void onChosenDir(String chosenDir) \r\n\t\t\t\t\t{\r\n\t\t\t\t\t\t\/\/ The code in this function will be executed when the dialog OK button is pushed\r\n\t\t\t\t\t\tm_chosen = chosenDir;\r\n\t\t\t\t\t\tToast.makeText(MainActivity.this, \"Chosen FileOpenDialog File: \" + \r\n\t\t\t\t\t\t\t\tm_chosen, Toast.LENGTH_LONG).show();\r\n\t\t\t\t\t}\r\n\t\t\t\t});\r\n\r\n\t\t\t\t\/\/You can change the default filename using the public variable \"Default_File_Name\"\r\n\t\t\t\tFileSaveDialog.Default_File_Name = \"my_default.txt\";\r\n\t\t\t\tFileSaveDialog.chooseFile_or_Dir();\r\n\r\n\t\t\t\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\r\n\t\t\t}\r\n\t\t});\r\n\r\n\t\t\/\/Button3\r\n\t\tButton dirChooserButton3 = (Button) findViewById(R.id.button3);\r\n\t\tdirChooserButton3.setOnClickListener(new OnClickListener() \r\n\t\t{\r\n\t\t\tString m_chosen;\r\n\t\t\t@Override\r\n\t\t\tpublic void onClick(View v) {\r\n\r\n\t\t\t\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\t\t\t\t\/\/Create FileOpenDialog and register a callback\r\n\t\t\t\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\t\t\t\tSimpleFileDialog FolderChooseDialog =  new SimpleFileDialog(MainActivity.this, \"FolderChoose\",\r\n\t\t\t\t\t\tnew SimpleFileDialog.SimpleFileDialogListener()\r\n\t\t\t\t{\r\n\t\t\t\t\t@Override\r\n\t\t\t\t\tpublic void onChosenDir(String chosenDir) \r\n\t\t\t\t\t{\r\n\t\t\t\t\t\t\/\/ The code in this function will be executed when the dialog OK button is pushed\r\n\t\t\t\t\t\tm_chosen = chosenDir;\r\n\t\t\t\t\t\tToast.makeText(MainActivity.this, \"Chosen FileOpenDialog File: \" + \r\n\t\t\t\t\t\t\t\tm_chosen, Toast.LENGTH_LONG).show();\r\n\t\t\t\t\t}\r\n\t\t\t\t});\r\n\r\n\t\t\t\tFolderChooseDialog.chooseFile_or_Dir();\r\n\r\n\t\t\t\t\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\r\n\r\n\t\t\t}\r\n\t\t});\r\n\r\n\t}\r\n\r\n\t@Override\r\n\tpublic boolean onCreateOptionsMenu(Menu menu) {\r\n\t\t\/\/ Inflate the menu; this adds items to the action bar if it is present.\r\n\t\tgetMenuInflater().inflate(R.menu.main, menu);\r\n\t\treturn true;\r\n\t}\r\n\r\n}\r\n\/\/<\/pre>\n<p><!--------------------------------- END CODE ----------------------------------><\/p>\n<p>For the New Folder button to actually create a new folder in the demo you need to include the following line in the Manifest file to give the app permission to write to the SD Card.<br \/>\n<code><\/code><\/p>\n<pre style=\"width: 94%; border: black 2px solid; color: black; background-color: #d0d0d0;\">&lt;uses-permission android:name=\"android.permission.WRITE_EXTERNAL_STORAGE\" \/&gt;<\/pre>\n<p>None of this code would have been possible without the jump start I got from the <a href=\"http:\/\/www.codeproject.com\/Articles\/547636\/Android-Ready-to-use-simple-directory-chooser-dial\">Simple Directory Chooser<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since there isn&#8217;t a built in file dialog interface for android development I have been..<\/p>\n","protected":false},"author":1,"featured_media":277,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/www.scorchworks.com\/Blog\/wp-json\/wp\/v2\/posts\/242"}],"collection":[{"href":"https:\/\/www.scorchworks.com\/Blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.scorchworks.com\/Blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.scorchworks.com\/Blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.scorchworks.com\/Blog\/wp-json\/wp\/v2\/comments?post=242"}],"version-history":[{"count":11,"href":"https:\/\/www.scorchworks.com\/Blog\/wp-json\/wp\/v2\/posts\/242\/revisions"}],"predecessor-version":[{"id":309,"href":"https:\/\/www.scorchworks.com\/Blog\/wp-json\/wp\/v2\/posts\/242\/revisions\/309"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.scorchworks.com\/Blog\/wp-json\/wp\/v2\/media\/277"}],"wp:attachment":[{"href":"https:\/\/www.scorchworks.com\/Blog\/wp-json\/wp\/v2\/media?parent=242"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.scorchworks.com\/Blog\/wp-json\/wp\/v2\/categories?post=242"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.scorchworks.com\/Blog\/wp-json\/wp\/v2\/tags?post=242"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}