Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] kotlin – Is it possible to get Android SD card location on SAMSUNG Tablet / Android 11

Posted on November 11, 2022 By

Solution 1 :

You can try this:

File[] dirs = getExternalFilesDirs("");
for (File currD :
        dirs) {
    if (currD.getAbsolutePath().equals(getExternalFilesDir("").getAbsolutePath())) {
        // Here is internal storage path
    } else {
        // Here is SD path
    }
}

Problem :

I have looked over many posts on getting SD card locations, and most that mention using Environment.getExternalStorageState(), Environment.getExternalStorageDirectory() always returns internal storage for my on a Samsung Galaxy Tab A / Android 11 (/storage/emulated/0)

I did see a comment that said to use System.getenv("SECONDARY_STORAGE"), which returns /storage/sdcard:/storage/usb1:/storage/usb2, but trying to open a file (I have put there) just returns a not found error (ie java.io.FileNotFoundException: /sdcard/external_sd/Music/test/testfile.mp3: open failed: ENOENT (No such file or directory).

Also tried Environment.getExternalStoragePublicDirectory(Environment.MEDIA_MOUNTED).getPath() and this returns /storage/emulated/0/mounted and this does not work either

I have the following permissions..

<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" />

and at startup I call

 private fun verifyStoragePermissions(activity: Activity) {
    // Check if we have write permission
    val permission =
        ActivityCompat.checkSelfPermission(activity, READ_EXTERNAL_STORAGE)

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
            activity,
            arrayOf(READ_EXTERNAL_STORAGE, WRITE_EXTERNAL_STORAGE),
            
        )
    }
}

Finally, as stated here, I hardwired /sdcard/external_sd and this did not work either (ie I try to open a file I know is there, and also using the following to list contents..

File(testFolder).walk().forEach {
            println(it)
        }

I have other application that writes to it, so there must be some way to do it (I only want to read existing files).

What might I be doing wrong?

Comments

Comment posted by CommonsWare

“I have the following permissions” — the latter two are not actual permissions.

Comment posted by peterc

This is close enough. First try I dismissed, as it never went into the else. But looking in dirs I could see two paths, and one of them was the storage card (/storage/3BDD-1A1D)

READ  [FIXED] java - Cannot create an instance of class of AndroidViewModel in android
Powered by Inline Related Posts

Comment posted by Александр Остапюк

Thanks i spent 2 days looking for a path to sd-card in android 12. And only here I found the correct answer

Android Tags:android, kotlin

Post navigation

Previous Post: [FIXED] android – How to detect when third party app’s dialog box appears in React native?
Next Post: [FIXED] android – Get the list of user SIM cards and select it – flutter

Related Posts

[FIXED] Local module descriptor class for com.google.android.gms.googlecertificates not found Android
[FIXED] android – Dealing with inner Transformations.switchMap on a viewModel Android
[FIXED] java – android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class Android
[FIXED] android – BLE advertising fails Android
[FIXED] xml – Hide layout if content is gone Android
[FIXED] retrofit2 – Android – Error Retrofit Multiple file upload Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • What color are dead flea eggs?
  • What is Indiana vine?
  • What’s the downside of a Chromebook?
  • Is phosphide the same as phosphorus?
  • Why do you need an S bend?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme