Scoped Storage Enforcement for reading and writing raw sensor data

Viewed 170

Currently, we have an app that we are targeting Android 10 and right now are using the legacy storage API. Our app communicates via Bluetooth sensors and reads and writes raw data in CSV files in a subfolder in the main directory, with that subfolder having subfolders for each user.

I know Android 11 will enforce Scoped Storage. I would like to know, is our use case outside of the Scoped Storage requirement? It appears our use case isn't supported by MediaStore. If not, how would we go about this?

3 Answers

MediaStore APIs are just for media files - images, videos, and audio.
You can store all files in the app's private folder and add an export option to your app (maybe compress the whole structure to an archive). So a user will be able to store or send it wherever they want.
In this case, you need to use FileProvider to expose the file from the private directory.

reads and writes raw data in CSV files in a subfolder in the main directory,

For an Android 11 device you can create your own folders an subfolders in the Documents directory of what you call the 'main folder'.

And for using the MediaStore: you can also write any file to that Documents directory. Well in a subfolder if not directly.

I'm in a similar boat. This may help you get started.

public class FirstFragment extends Fragment {
...
public void fauxMakeCsvSurveyFile() {
    File appDir = new    File(getContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS),   "Field_data");
    appDir.mkdirs();
try {
 String storageState = Environment.getExternalStorageState();
        if (storageState.equals(Environment.MEDIA_MOUNTED)) {
          File file = new File(getContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + "/Field_data/" + "OutputFile.csv");
            FileOutputStream fos = new FileOutputStream(file);
            String text = "Hello, world!";
            fos.write(text.getBytes());
             fos.close();
            }
    }   catch (IOException e) {
Log.e("IOException", "exception in createNewFile() method");
    }
   }
...
}
Related