FYI: How to reliably identify an Android Tablet microSD Card

Peter Reid preid at reidit.co.uk
Wed Dec 19 06:37:16 EST 2018


FYI

I've managed to put together some code that reliably locates the microSD card in 4 very different Android-based tablets. The approach involves 3 different methods:

1. Search for a known folder on the SD card starting in the standard "/mnt" folder using a breadth-first search strategy.

2. Search for a known folder on the SD card starting in the less standard "/storage" folder using a breadth-first search strategy.

3. Search for a known folder using full paths that are used by specific manufacturers. These look as though they should be located by method 1. (they are all paths from "/mnt") but sometimes fail 1. however they work if the full path is specified!

Here's my code for "findSDcard"...

----------------

on findSDcard
   constant \
      cSDcardRoot1 = "/mnt/ext_sdcard/Android/data/com.myorg.myapp/files", \
      cSDcardRoot2 = "/mnt/sdcard2/Android/data/com.myorg.myapp/files", \
      cSDcardRoot3 = "/mnt/m_external_sd/Android/data/com.myorg.myapp/files", \
      cSDcardRoot4 = "/mnt/sdcard/Android/data/com.myorg.myapp/files", \
      cSDcardRoot5 = "/mnt/extSdCard/Android/data/com.myorg.myapp/files", \
      cSDcardRoot6 = "/mnt/external_sd/Android/data/com.myorg.myapp/files", \
      cSDcardRoot7 = "/mnt/SDCard/Android/data/com.myorg.myapp/files"
      
    global gDataPath -- /myapp-data
    global gSDcard -- path to app's data folder

   set wholeMatches to true
   put empty into gSDcard
   put (char 2 to -1 of gDataPath) into tTargetFolderName
   if the platform is "android" then
      put "/mnt" into tDeviceRoot
      if there is a folder tDeviceRoot then
         -- search in standard /mnt folders:
         put empty into tFoundPath
         folderExists tTargetFolderName, tDeviceRoot, tFoundPath
         if tFoundPath is not empty then
            put tFoundPath into gSDcard
         end if
      end if
      if gSDcard is empty then
         -- try non-standard /storage folders:
         if there is a folder "/storage" then
            -- search in non-standard /storage folders:
            put "/storage" into tDeviceRoot
            put empty into tFoundPath
            folderExists tTargetFolderName, tDeviceRoot, tFoundPath
            if tFoundPath is not empty then
               put tFoundPath into gSDcard 
            end if
         end if
      end if
      if gSDcard is empty then
         -- try bespoke SD locations:
         if there is a folder cSDcardRoot1 then
            put cSDcardRoot1 into gSDcard    -- Huawei
         else if there is a folder cSDcardRoot2 then
            put cSDcardRoot2 into gSDcard    -- Amazon
         else if there is a folder cSDcardRoot3 then
            put cSDcardRoot3 into gSDcard    -- Lenovo
         else if there is a folder cSDcardRoot4 then
            put cSDcardRoot4 into gSDcard    -- Fusion
         else if there is a folder cSDcardRoot5 then
            put cSDcardRoot5 into gSDcard    -- ??
         else if there is a folder cSDcardRoot6 then
            put cSDcardRoot6 into gSDcard    -- ??
         else if there is a folder cSDcardRoot7 then
            put cSDcardRoot7 into gSDcard    -- Hudl?
         end if
      end if
   end if
end findSDcard

on folderExists pFolderName, pRootPath, @pFoundPath
   constant cNoDotsRegex = "^\..*"
   
   -- breadth-first search for a given folder
   put empty into pPath
   if there is a folder pRootPath then
      put empty into tSubFolders
      put folders(pRootPath) into tFolderList
      filter lines of tFolderList without regex cNoDotsRegex
      repeat for each line tFolder in tFolderList
         if tFolder = pFolderName then
            -- found it:
            put pRootPath into pFoundPath
            exit folderExists
         else
            -- add sub-folder to list for checking:
            put pRootPath & "/" & tFolder & return after tSubFolders
         end if
      end repeat
      if tSubFolders is empty then
         exit folderExists
      end if
      repeat for each line tSubFolder in tSubFolders
         folderExists pFolderName, tSubFolder, pFoundPath
         if pFoundPath is not empty then
            exit folderExists
         end if
      end repeat
   end if
end folderExists

----------------

Note that the above was necessary because specialFolderPath("external documents") doesn't work reliably across the 4 Android-based tablets I'm trying to support.

If anyone has a more elegant or succinct method, please let me know.  In the meantime you're welcome to use the above code.

Peter
--
Peter Reid
Loughborough, UK

> On 28 Nov 2018, at 11:49am, Peter Reid <preid at reidit.co.uk> wrote:
> 
> I've got an app running on an Android 8.1 7in tablet which uses a microSD card for its RW data storage. I've tried using:
> 
> 	specialFolderPath("external documents")
> 
> which the docs describe as follows:
> 
> 	• "external documents": The folder on the primary shared/external storage device where application-specific data can be placed
> but this doesn't point to the microSD.
> 
> Instead I'm having to try to pause the app and then search around for the microSD.  So far I've found 7 different locations for the microSD, depending on the model of tablet:
> 
> 	1. /mnt/ext_sdcard/Android/data/com.reidit.myapp/files
> 
> 	2. /mnt/sdcard2/Android/data/com.reidit.myapp/files
> 
> 	3. /mnt/m_external_sd/Android/data/com.reidit.myapp/files
> 
> 	4. /mnt/sdcard/Android/data/com.reidit.myapp/files
> 
> 	5. /mnt/extSdCard/Android/data/com.reidit.myapp/files
> 
> 	6. /mnt/external_sd/Android/data/com.reidit.myapp/files
> 
> 	7. /mnt/SDCard/Android/data/com.reidit.myapp/files
> 
> where the ../files folder is located in the microSD filing system as follows:
> 
> 	/Android
> 		/data
> 			/com.reidit.myapp
> 				/files
> 
> Whenever I try to install my app on a different Android tablet, I have to go hunting for the exact filing system for the 1st part of the microSD:
> 
> 	/mnt/<unknown>/
> 
> The tail of /Android/data/com.reidit.myapp/files seems to be consistent. It seems that the only variable is the <unknown> part.
> 
> However, I have 2 tablets (Android 5.1 and Android 8.1) where I haven't been able to locate the microSD card at all!
> 
> Any suggestions about how to reliably locate the microSD card in an Android tablet?
> 
> Peter
> --
> Peter Reid
> Loughborough, UK
> 





More information about the Use-livecode mailing list