Android - all file access permission - how to get sd card data

301 Views Asked by At

I'm creating an app for my own use case which is following. I want to sort photos to the folders by date creation. eg. Folders with names like 2023-01, 2023-02 and put photos there sorted by creation time.

I have photos on sd-card The problem: I can not access sd card. I have permission ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION granted. But nothing from the following gives me path to the sdcard:

context.getExternalFilesDirs(null) gives me array with two object but one of them is null

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) points to internal storage

File("/sdcard") at first it looks like it works, but all data are from internal storage not sd card

I need to get some path like this: /storage/7B32-B4556/

This is really frustrating for me. I have also tried runnning

  val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
  chooseDirectoryRequestLauncher.launch(intent)

but it gives me some obsucre "content" uri and I'm not able to get absolute file path from it. I do not want to use storage api.

This is the api I'm targeting to

 compileSdk 34

    defaultConfig {
        applicationId "com.cndgf.photosorter"
        minSdk 30
        targetSdk 34
        versionCode 1
        versionName "1.0"

this is how the permissions in manifest looks like:

    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.PERMISSIONS_STORAGE" />
    <uses-permission android:name="android.permission.REQUEST_EXTERNAL_STORAGE" />

Thank you

3

There are 3 best solutions below

0
ferdinand On BEST ANSWER

I guess this is what I was looking for

            val storageService = context.getSystemService(Context.STORAGE_SERVICE) as StorageManager
            val volumes = storageService.storageVolumes

it gives also sd card location

2
BambiHax On

Here's a step-by-step guide on how to organize your photos by creation date using the MediaStore API without directly accessing the SD card:

Request the necessary permissions:

Make sure your app's manifest includes the READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE permissions. If your app targets Android 6.0 (Marshmallow) or above, you'll need to request these permissions at runtime as well. Use the MediaStore API to retrieve the photos' metadata:

The MediaStore API allows you to access media files, such as photos, regardless of their storage location. You can query the MediaStore to fetch the necessary information, such as the photo's ID, file path, and creation date. Iterate through the retrieved data:

Once you have the cursor from the MediaStore query, loop through the data to process each photo. For each photo, extract its file path (DATA) and creation date (DATE_TAKEN). Organize the photos into folders based on their creation date:

Create a folder structure that matches your desired format, such as "YYYY-MM" (e.g., "2023-01"). Extract the year and month from the creation date of each photo. Use these values to construct the destination directory path where the photo will be moved. Move the photos to the respective folders:

Once you have the source file path and the destination directory path, move the photo from its current location to the appropriate folder. Use file manipulation operations (e.g., renaming or copying) to move the photo to the desired folder. Repeat this process for each photo in the loop. Remember to handle any exceptions or errors that may occur during the process and provide appropriate feedback or error handling to the user.

By following these steps, you can organize your photos by their creation date without directly accessing the SD card.

0
blackapps On

it gives me some obsucre "content" uri and I'm not able to get absolute file path from it.

There is nothing obscure on a content scheme uri for a directory. You can just further use SAF to list the files in that directory. And to read those files.

No need for a file system path. Which does not even exists when getExternalFilesDirs() does not deliver it.

You do not need any permission to use SAF.