How to transfer data in files from different locations to app directory

In Android apps, we might have seen various instances in-app where we can import, export or save files. Now, where does the files go to or come from? There needs to be a specific folder or directory (maybe root) for a particular app which inturn contains the necessary files, the user has worked with from the app. In this blog, we will be learning about the backend of how to create directories and store files in them dynamically with proper content. Context I have been working with FOSSASIA on the project Neurolab-Android. In the app, we have various program modes, which has the option to save/record data. The saved data gets logged in a new file in the Neurolab directory/folder. Feel free to go ahead and explore the feature. The Save/Record feature in Neurolab can be found in the app bar or as an option in the drop down menu present in the app bar itself in any program mode. The feature becomes functional, once the data is imported with the import data feature which is also present in the app bar.                                               Figure: Demonstration of features in Neurolab Tutorial Now, starting off, there are apps out there wherein users can save files from different segments in the app and those saved files can be used by the app itself at other times as they belong to that app itself specifically. First off, we need to make sure we have a directory for our app, wherein the files will get stored. If the directory or folder is not present, it needs to be created. File directory = new File( Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + DIRECTORY_NAME); if (!directory.exists()) { try { directory.mkdir(); } catch (Exception e) { e.printStackTrace(); } } Now, once the directory is present, we can go ahead to keep the saved files in it. Also we will be implementing category-wise storage of the files. Simply put, we will be storing csv files in the CSV folder, xls files in the Excel folder, etc. In the below code example, we see the use case of CSV ‘category’. private void categoryWise() {File categoryDirectory = new File( Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + DIRECTORY_NAME + File.separator + category); if (!categoryDirectory.exists()) { try { categoryDirectory.mkdir(); } catch (Exception e) { e.printStackTrace(); } }} For saving a file in our app, we need to get (import) the file into our app. Once the file is imported, we can get the path of the file with the help of its URI. Let’s assign the path to a variable named ‘importedFilePath’. Then we place the imported file in the required category directory within our parent app directory depending and deciding upon the extension of the imported file. File importedFile = new File(importedFilePath); FilePathUtil.setupPath(); Date currentTime = Calendar.getInstance().getTime(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String fileName = sdf.format(currentTime); File dst = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + DIRECTORY_NAME + File.separator + categoryDirectory + File.separator + fileName + ".$extension"); if (!dst.exists()) { try { categoryWise() transfer(importedFile, dst); } catch (IOException e) {…

Continue ReadingHow to transfer data in files from different locations to app directory