External SD Card Root Folder

tdocs2

Well-Known Member
Licensed User
Longtime User
How do I get the name of the External SD Card Root Folder? I have searched for hours and experimented with my own code, but to no avail.

The File.DirRootExternal returns the internal storage root folder name.

My objective is:

In any device, I want to get a file list in a folder in the external SD card.
I can do this in the internal storage by using File.DirRootExternal.

Thank you.
 

Jost aus Soest

Active Member
Licensed User
Longtime User
Of course "all circumstances" means all valid fstab-files. Therefor the quotation marks! ;)

/mnt/sdcard-ext:none:lun1

Thanx for this information! This is really a strange one, because the colon is definitely not a whitespace character... :D

So we have to add this line to getSDCardPath
B4X:
fstab = fstab.Replace(":", " ") 'remove colon characters (Motorola-case)
to handle the unspecified Motorola-case.
 
Last edited:
Upvote 0

birnesoft

Active Member
Licensed User
Longtime User
SDCard detection

How can I detect if a SDCard is inside or mass-storage is off


I tried this easy way, but it crashes when there is no sdcard or no files in the folder:

Dim li As List
li.Initialize()
li=File.ListFiles("/mnt/sdcard")
If li.size=0 Then ToastMessageShow("no SDCARD",False)

so I tried "try catch"
but ss is everytime 0

Dim li As List

Dim ss As Int
li.Initialize()
li=File.ListFiles("/mnt/sdcard")

Try
ss=li.Size
Catch
ss=-1
End Try

If ss=-1 Then ToastMessageShow("no SDCARD",False)
 
Upvote 0

mjtaryan

Active Member
Licensed User
Longtime User
I think you got it Margret, however, there are a few of things to consider.

1- To be able to write to the external SDCard you have to add this line to the manifest:
B4X:
AddPermission("android.permission.WRITE_EXTERNAL_STORAGE")

2- Devices without an external SDCard will return an empty list (e.g. Nexus 7), so, these lines should be added after the For...Next in Sub DevicePaths
B4X:
If dPaths.Size = 0 Then
   dPaths.Add("/mnt/sdcard, /mnt/sdcard")
End If

3- On ICS and up you CANNOT write to the external SDCard, I think Google changed the permissions, so, having something something like:
B4X:
File.OpenOutput(SGW(ChkVal, 3), "test.txt", False)
 
Or
 
File.OpenOutput("mnt/sdcard/ext_sd", "test.txt", False)

Will show a "FILE NOT FOUND" error (Assuming you added the permissions in item 1, otherwise you'll get File Not found + Permission denied).

I have read about a workaround but it requires root access. Well, at least you solved part of the problem and we learned from it.


Under #3 above, that is not completely true. On one of my devices, which is running JellyBean and is not rooted, I can read and write to the external SDCard. However, in the app I built I specifically know the path - that is I've not had to search for it using the method described in this thread.

A problem I've found in using DirExternalRoot (or DirRootExternal) is that it often returns a path to the internal storage (called SDCard). Thus, a false postive is generated. I really wish Google would fix Android so that it requires a standard and universal reference or value for each of the posible storage mount points.

I did come across a thread in another forum that discusses a solution to finding the ExternalSDCard. The solution is written in Java (I think - I'm not familiar with Java). The discussion is located HERE. There is a post with code that calls a procedure (method) "Environment" and some of its submethods. Perhaps someone who knows Java can figure out a way of an adaptation of the posted code from within B4a??
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
I agree. #3 is wrong. Maybe Margret has a limitation on her device of some kind, but it's not a rule at all with ICS or Jellybean.

The code that you mention is of no help. It's the usual way to retrieve the path of an external card, the same that is used by Basic4Android for its functions.
 
Upvote 0

mjtaryan

Active Member
Licensed User
Longtime User
I agree. #3 is wrong. Maybe Margret has a limitation on her device of some kind, but it's not a rule at all with ICS or Jellybean.

The code that you mention is of no help. It's the usual way to retrieve the path of an external card, the same that is used by Basic4Android for its functions.

Yes, but the code looked like it makes additional info retrieveable that might help in resolving the ICS, JB and future versions problems of the nature we are discussing.

Using Margaret's concept as an initial starting point, I did some playing around over the weekend. The results of my quick and dirty testing suggest that Android was modified rather than the idiosynchrosy being that of the manufacturer, but I could be wrong. I have four devices. Unfortunately, for this purpose three of them are by the same manufacturer (Samsung) and the other is HTC (Evo 4g). Two of the devices are running Android 2.3 (the Evo has 2.3.3 and a Galaxy Proclaim has 2.3.6). With both of these devices "Files.DirRootExternal" DOES return the actual user supplied SD card. However, the other two Devices (Samsung Note II running 4.1.2 and a Note 10.1 running 4.0.3) are different. On both these devices "Files.DirRootExternal" returns the internal storage root and not the user supplied SD card. To this test I added some lines (similar to Margaret's) that search for the Android subfolder on the user's SD card. The search employs the patterns "/mnt/sdcard/Android", "/storage/sdcard/Android", "/mnt/extSdCard/Android" and "/mnt/extSdCard/Android". With the devices running 2.3 the path to the user's SD Card and that returned by "Files.DirRootExternal" are the same. However, for the two devices running 4.x the user's SD Card returns "/mnt/extSdCard/Android" or "/storage/extSdCard/Android" (depending on the order of the search) but "Files.DirRootExternal" returns "/storage/sdcard0/Android" (internal storage). If none of the search variations produce a result, the test assumes a user supplied SD card is not available.

The attached zip file contains the quick b4a test app souce code and two text files with the results of the initial test and the search for the user's SD card. I'm hoping people with devices by other manufacturers and other versions of Android will run it on their devices and report the results. Be warned that there is no output to the device's screen with the exception of a messagebox in the event that none of the search patterns produce a result. I hope this is useful.
 

Attachments

  • ExtRootTest.zip
    398.7 KB · Views: 624
Upvote 0

Marcob

Member
Licensed User
Longtime User
Upvote 0

HotShoe

Well-Known Member
Licensed User
Longtime User
The only standard for naming external (add-on) sd cards has been, "there is no standard". That said, most have adopted the /storage/extSdCard/ as a mount point in recent versions of Android. /mnt/extSdCard is sometimes a symlink to that directory and the built in file handling may not deal with links. I have a new version of my MLfiles library that I am finishing up, but the old one deals with links just fine. If you can wait for a day, the new one will be worth it.

In the mean time, you can try using File.ListFiles("/storage/extSdCard") to see if you can access it directly. You can also try external_sd on some older devices and extsd (or some variation in capitalization).

--- Jem
 
Upvote 0
Top