You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Android 11 Scoped Storage强制机制下原始传感器数据读写场景的合规性与适配方案咨询

Hey there! Let's break down your questions about Scoped Storage on Android 11 clearly:

First off, your use case does NOT qualify for a Scoped Storage exemption. Exemptions are reserved for very specific app types like dedicated file managers, backup utilities, or accessibility services—your app handling Bluetooth sensor CSV data doesn't fall into those categories. And you're totally right about MediaStore being unsuitable here; it’s built for media files (images, audio, video), not structured sensor data in CSV format.

Now, let’s dive into practical adaptation solutions to comply with Scoped Storage rules:

This is the simplest, most compliant approach, and it doesn’t require any extra permissions.

  • How it works: Every Android app has its own private storage directory that other apps can’t access. The system automatically deletes this data when your app is uninstalled, which aligns with Android’s security guidelines.
  • Implementation snippet:
    // Get the app's external private storage directory
    File appPrivateDir = context.getExternalFilesDir(null);
    // Create a root folder for all sensor data
    File sensorRootDir = new File(appPrivateDir, "SensorData");
    // Create a subfolder for a specific user
    File userSpecificDir = new File(sensorRootDir, "user_12345");
    // Ensure all directories exist
    userSpecificDir.mkdirs();
    // Create a CSV file for the user's sensor readings
    File csvFile = new File(userSpecificDir, "2024_05_20_sensor_log.csv");
    
  • Pros: No need for READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE permissions, clean and simple code, fully compliant with Android’s rules.
  • Cons: Data gets deleted when the app is uninstalled. If users need to export their sensor data, you’ll need to add a small export feature (e.g., using Intent.ACTION_SEND to share the CSV file with other apps like email or file managers).

2. Use the Storage Access Framework (SAF) for User-Selected Directories

If you need sensor data to persist after app uninstall or want to let users share data with other apps, SAF is the way to go.

  • How it works: You’ll prompt users to pick a directory via Android’s built-in file picker. Once they grant permission, your app can create subfolders and write CSV files in that location.
  • Implementation steps:
    1. Launch the SAF directory picker:
      Intent pickDirIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
      startActivityForResult(pickDirIntent, REQUEST_CODE_PICK_STORAGE_DIR);
      
    2. Handle the result and persist permissions (so users don’t have to re-select the directory every time):
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          if (requestCode == REQUEST_CODE_PICK_STORAGE_DIR && resultCode == RESULT_OK) {
              Uri selectedDirUri = data.getData();
              // Persist the permission for future app launches
              getContentResolver().takePersistableUriPermission(selectedDirUri,
                      Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
              // Create the sensor data root folder
              DocumentFile rootDir = DocumentFile.fromTreeUri(this, selectedDirUri);
              DocumentFile sensorDataDir = rootDir.createDirectory("SensorData");
              // Create a user-specific subfolder
              DocumentFile userDir = sensorDataDir.createDirectory("user_12345");
              // Create the CSV file
              DocumentFile csvFile = userDir.createFile("text/csv", "2024_05_20_sensor_log.csv");
              // Write data using getContentResolver().openOutputStream(csvFile.getUri())
          }
      }
      
  • Pros: Data remains intact after app uninstall, users can choose their preferred storage location, supports sharing data with other apps.
  • Cons: Requires user interaction to select the directory, and the code is a bit more complex compared to app-specific storage.

3. Legacy Storage for Android 10 and Below (Temporary Fix)

If you need to maintain compatibility with Android 10 or older, add this attribute to your AndroidManifest.xml:

<application
    ...
    android:requestLegacyExternalStorage="true">
    ...
</application>

This lets your app use the old Legacy Storage model on Android 10, but it has no effect on Android 11+—you’ll still need to implement one of the above adaptation options for newer OS versions.

To wrap up: Start with app-specific storage if it meets your needs, since it’s the most straightforward. If data persistence or sharing is a must, go with SAF. Use the legacy flag only as a temporary bridge for older Android versions.

内容的提问来源于stack exchange,提问作者Pink Jazz

火山引擎 最新活动