This blog demonstrates how a user can import log files from local storage to the PSLab Android application for various instruments and play them. This functionality is really useful as users can share their log files and import them in their app. This blog mostly consists of my work in the PSLab Android repository.
How to access local storage files?
We here use the concept of implicit intent to access the local storage of the device and then generate the file from the received data URI.
Implicit intents differ from explicit intents in a way that, they don’t give exact class or activity to be initialized through the intent, instead they provide the action to be performed and the class or activities are selected implicitly from the required action
The code block is shown below.
Here the Intent.ACTION_GET_CONTENT defines implicit intent. This intent opens the activity related to the action of GETTING CONTENT. The type of content is specified in the Intent.setType(<TYPE>). Since here the type is set to “*/*”, it will open all types of files. If we want only images we can set Type to “images”.
startActivityForResult(intent, <REQUEST_CODE>) starts the file selection activity.
How to generate a file from received URI?
Once the user selects a file from the file selection activity we can generate the selected file from the data passed in the callback function of startActivityForResult(). The data intent passed as a parameter to onActivityResult() callback contains data for the selected file. We can retrieve path, name, etc details of the selected file from this data intent. The code block for the same is given below.
Here we check for the requestCode, which we passed when calling the startActivityForResult() function. We further check if the result is valid and then generate the file from the file path we received in the data Intent. Once we get the path we can get the selected file using the following lines of code:
How to get Data from the file?
Once the file is generated, it is passed to a function getFileData(File file) to get data in the file to add to the logs of the selected device. The main part of the getFileData function is given below.
Here we read the file line by line and convert the CSV data into the object of the selected device. And then this data is added to app storage using the realm. As shown in the code block above, we are parsing the data to the BarometerData class instances. We split each line by “,” and then use each field as input to the constructor of the BarometerData class. Once we create the instances of the class, we add them to the realm, so the imported file is saved in the realm and now we can access it easily from DataLoggerActivity.
The following images demonstrate the functionality of Import log
Step 1: Select Import Log menu from
Step 2: Select the file to be imported from the local storage
Step 3: Play the imported log from the DataLoggerActivity
- Android Documentation on Intent and intent filters
- Create a CSV file in android
- Data logging in PSLab
- CSV Logger in PSLab project repo
- Tutorial on Intents
Tags: PSLab, Android, GSoC 19, ImportLog, Intents, Implicit Intent