Some clarification about writing to external storage (sd card)

jschuchert

Active Member
Licensed User
Longtime User
I have been testing all the aspects of my application, including writing to an external file. The following code is representative of what I have been using.
B4X:
If chkmultinvWriter.Checked =True Then
filename=txtfilename.Text 
Msgbox("A file named " & filename & " will be created for your use. See manual for details","Write to sd card")
writer.Initialize (File.OpenOutput (File.DirRootExternal ,filename,True))
writer.Write("MULTIPLE INVERSE ROUTINE" & CRLF & CRLF)
writer.Write ("Point range = " & txtrange.Text & CRLF )
writer.Write ("Inverses " & CRLF )
writer.Write(txtdisplay.Text )
writer.Close 
End If

I had assumed since it was being written to 'dirrootexternal' that it was being written to the sdcard. I found out that is not the case. It is still internal on the device. That's fine because when I connect via usb, I can transfer those files to my computer and it works great. So if the internal storage of the device is full, how would files be written to the sd and bypass internal? I am guessing that the typical use of an sd would be to transfer music, videos and movies to a device. My tablet has 4gb of internal storage so I doubt I will ever need anything else but I suppose I am not a typical user. Thank goodness I have a device to work with to learn all these things before I release my app to the public.

Jim
 

dbuchman

New Member
Licensed User
Longtime User
Mororola Droid 3 sd paths

I can write to these paths on my Droid 3.
"/mnt/sdcard" - internal sd card (non removeable)
"/mnt/sdcard-ext" - external sd card (removeable)
 
Upvote 0

latcc

Banned
You are right. The absolute address method is the ONLY way to address the many tablets (and some phones) out there with non-'standard' root/path names to the micro-SD card. The standard method catches many devices but not all of them. I agree with you. We need to build a list of Root/Path names for removable storage cards for non-standard devices.

Well done for pursueing this issue. :)


'------------------------------------
' LG 3D Tablet
'------------------------------------
root = "/mnt"
folder = "sdcard/_ExternalSD"

'------------------------------------
' Flytouch 3 Tablet
'------------------------------------
root = "/mnt"
folder = "sdcard1"

'------------------------------------
' Flytouch 3 Tablet #2
'------------------------------------
root = "/mnt"
folder = "sdcard/sdcard1"

'------------------------------------
' Scroll Essentials ablet
'------------------------------------
root = "/mnt"
folder = "sdcard/tflash"
 
Last edited:
Upvote 0

mjtaryan

Active Member
Licensed User
Longtime User
Now what I'm wondering is whether this solution can be generalized across devices and manufacturers?
 
Upvote 0

jimcashby3

Member
Licensed User
Longtime User
This worked for me Samsung GT-P3113 7" tablet

'************************************************************************************
' Load the working directory for file access
'************************************************************************************
WorkingDirectory = "/mnt/sdcard/Android/data/IST.DISTO/files/"
'************************************************************************************
 
Upvote 0

jimcashby3

Member
Licensed User
Longtime User
I entered and ran
Dim filestring = File.DirDefaultExternal As String
Log(filestring)

It returned
/storage/emulated/0/Android/data/IST.DISTO/files

Is this really the SD Flash card?
 
Upvote 0

jsanchezc

Member
Licensed User
Longtime User
Thanks for information.
I had same problem, i did an app to take photo and save.
Internal SD has 2Gb and external SD can have up to 32Gb.

I did a sub mixing your information, added some path (sdcard3,sdcard2...)
and added some ucase and lowcase (SD,sd ...):

B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Dim ExtRoot As String 'The obtained root directory for the actual physical external SD card (user supplied).
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
' Remove the Log lines.
 
    ExtRoot=GetSDCardFolder
    Log(" ")
    Log(" ")
    Log("GetSDCardFolder says: Ext Root directory = " & ExtRoot)
    Log(" ")
    ToastMessageShow( "GetSDCardFolder says: Ext Root directory = " & ExtRoot,True)
    Activity.Finish
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub
Sub GetSDCardFolder() As String
'*************************************************************
'Try to find the location of removable external sdcard memory,
'perform a write test and return the folder name, if writable.
'*************************************************************
Dim root As String=""
Dim folder As String=""
Dim SDf As String=""
  '... 3
  root = "/mnt"
  folder = "sdcard3"
  SDf=TryExternalSD(root,folder)
  If SDf<>"" Then Return SDf
  '---------------------------------------
  ' ... 2
  '---------------------------------------
  root = "/mnt"
  folder = "sdcard2"
  SDf=TryExternalSD(root,folder)
  If SDf<>"" Then Return SDf
  '----------------------------------------
  'Quechua 5
  '----------------------------------------
  root = "/mnt"
  folder = "sdcard1"
  SDf=TryExternalSD(root,folder)
  If SDf<>"" Then Return SDf
  '----------------------------------------
  '... 0
  '----------------------------------------
  root = "/mnt"
  folder = "sdcard0"
  SDf=TryExternalSD(root,folder)
  If SDf<>"" Then Return SDf
  '----------------------------------------
  'porsi nada
  '----------------------------------------
  root = "/mnt"
  folder = "sdcard"
  SDf=TryExternalSD(root,folder)
  If SDf<>"" Then Return SDf
   '----------------------------------------
  '"/mnt/extSdCard"
  '----------------------------------------
  root = "/mnt"
  folder = "extSdCard"
  SDf=TryExternalSD(root,folder)
  If SDf<>"" Then Return SDf
  '-------------------------------------------
  root="/storage"
  folder="/extSdCard"
  SDf=TryExternalSD(root,folder)
  If SDf<>"" Then Return SDf
  '-------------------------------------------
  root="/storage"
  folder="/sdcard"
  SDf=TryExternalSD(root,folder)
  If SDf<>"" Then Return SDf
  '------------------------------------
  ' Acer A500 Tablet
  '------------------------------------
  root = "/mnt"
  folder = "external_sd"
  SDf=TryExternalSD(root,folder)
  If SDf<>"" Then Return SDf
  '------------------------------------
  'Samsung Galaxy S2
  '-------------------------------------
  root = File.DirRootExternal
  folder = "external_sd"
  SDf=TryExternalSD(root,folder)
  If SDf<>"" Then Return SDf
  '-------------------------------------
  ' ASUS TF 101 (Micro SD)
  '-------------------------------------
  root = "/mnt/sdcard/Removable"
  folder = "MicroSD"
  SDf=TryExternalSD(root,folder)
  If SDf<>"" Then Return SDf
  '------------------------------------
  'Asus TF 101 (SD-Card Keyboard Dock)
  '------------------------------------
  root = "/mnt/sdcard/Removable"
  folder = "SD"
  SDf=TryExternalSD(root,folder)
  If SDf<>"" Then Return SDf
  '-------------------------------------
   root = File.DirRootExternal
  folder = "SD3"
  SDf=TryExternalSD(root,folder)
  If SDf<>"" Then Return SDf
  '-------------------------------------
   root = File.DirRootExternal
  folder = "SD2"
  SDf=TryExternalSD(root,folder)
  If SDf<>"" Then Return SDf
  '-------------------------------------
   root = File.DirRootExternal
  folder = "SD1"
  SDf=TryExternalSD(root,folder)
  If SDf<>"" Then Return SDf
  '-------------------------------------
   root = File.DirRootExternal
  folder = "SD0"
  SDf=TryExternalSD(root,folder)
  If SDf<>"" Then Return SDf
  '------------------------------------
  root = "/mnt/sdcard/Removable"
  folder = "sd"
  SDf=TryExternalSD(root,folder)
  If SDf<>"" Then Return SDf
  '-------------------------------------
   root = File.DirRootExternal
  folder = "sd3"
  SDf=TryExternalSD(root,folder)
  If SDf<>"" Then Return SDf
  '-------------------------------------
   root = File.DirRootExternal
  folder = "sd2"
  SDf=TryExternalSD(root,folder)
  If SDf<>"" Then Return SDf
  '-------------------------------------
   root = File.DirRootExternal
  folder = "sd1"
  SDf=TryExternalSD(root,folder)
  If SDf<>"" Then Return SDf
  '-------------------------------------
   root = File.DirRootExternal
  folder = "sd0"
  SDf=TryExternalSD(root,folder)
  If SDf<>"" Then Return SDf
  '-------------------------------------
  'HTC Desire
  '-------------------------------------
  root = File.DirRootExternal
  folder = "sd"
  SDf=TryExternalSD(root,folder)
  If SDf<>"" Then Return SDf
   '-------------------------------------
  'MOTOROLA DROID 3
  '-------------------------------------
  root = "/mnt"
  folder = "sdcard-ext"
  SDf=TryExternalSD(root,folder)
  If SDf<>"" Then Return SDf
  'to be continued....
  'return something real
  Return File.DirRootExternal
End Sub
Sub TryExternalSD(Root As String, Folder As String) As String
    Dim Result As String=""
    Dim SDf As String =""
    If File.IsDirectory(Root,Folder) Then
       SDf=Root & "/" & Folder
    If WriteTest(SDf) Then
       Return SDf
       Else
       Return ""
    End If
  End If
 
End Sub
Sub WriteTest(dirname As String) As Boolean
Dim s As String 
  Try
    File.WriteString(dirname,"test.tmp","test")
    s=File.ReadString(dirname,"test.tmp")
    File.Delete(dirname,"test.tmp")
  Catch
    Log(LastException.Message)
    Return False
  End Try
  If s="test" Then
     Return True
  Else
     Return False
  End If  
End Sub
 
Last edited:
Upvote 0

Aenaes

New Member
Licensed User
Longtime User
On my Samsung Galaxy Tab 3 (SM-T315), running Android 4.4.2:
File.DirRootExternal returns /Storage/Emulated/0/
File.DirDefaultExternal returns /Storage/Emulated/0/Android/data/my.package.name/files/
Neither of those is the external SD card.

However, after two days of struggle, I found I can access the SD card for writing by using:
"/storage/extSdCard/Android/data/" & Application.PackageName & "/files"

I'm using the default values in the manifest file - presumable B4A has added the required permissions?
 
Upvote 0
Top