Writing to external storage (sd card)

jschuchert

Active Member
Licensed User
Longtime User
Earlier I was able to write a file to the external sd card and transfer it to my computer using the usb connection. Now it won't do it. When I plugged in the usb cable earlier (when it was successful) it showed the "J" drive. Now it is the "K" drive but I didn't change anything. Here is a little of the code I am using:

B4X:
Sub btnOK_click
If chklistdisplay.Checked =True Then
Msgbox("A file named 'List of points.txt' will be created on the sd card","msg")
Dim writer As TextWriter
writer.Initialize (File.OpenOutput (File.DirRootExternal ,"listofpoints.txt",True))
End If

Then later on in the routine is this:

B4X:
.
.
.
txtdisplay.Text =txtdisplay.Text & cline(0) & "  " & cline(1) & "  " & cline(2) & "  " & cline(3) & CRLF
If chklistdisplay.Checked =True Then
writer.Write(txtdisplay.Text )
End If

It will not even create the file, much less its contents. Any ideas? I know it works because I saw it but now it doesn't. Thanks for any help or suggestions.

Jim
 

kickaha

Well-Known Member
Licensed User
Longtime User
Dont know if this is the issue, but for most (if not all) devices, when you are plugged in to the USB and you "mount" the device so that you can transfer files from the SD card, the device will no longer see the SD card.
 
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
Thank you, Andy. I did it both ways (mounted and unmounted,..I think) but had the same problem. Now I found the files I thought were being saved on the sd card...in the internal storage...using the code I posted before. How did that happen? I thought I saw somewhere that "sdcard" needed to be part of the path but I can't find it again.

Jim
 
Upvote 0

mjtaryan

Active Member
Licensed User
Longtime User
My (limited) experience has been that in order for the SD card to be recognized by the USB host system so that data can be transferred back and forth, the device contaning the SD must be in Disk Drive mode. This causs the host to "see" the device as an external drive and the SD card as the disk. Because it is "attached" to the host after the fact, it is assigned the next available drive letter -- this is usually done dynamically. During the time the devce is in Disk Drive mode the SD card is hidden from the Android operating system and is unavailable to any apps. At least that's howi it works on my Evo.

I assume from this that you cannot do both simultaneously. Logically this suggests that you will need to write/read to/from the SD card whie the device is in Charge Only mode (or some other mode) and then switch to Disk Drive to move the files to the host computer.

The only other option would be to set up the host as a server and have your app copy the file to the server. However, I haven't learned to do that yet in B4a. I'm still trying to get a handle on Intents :confused:
 
Upvote 0

jschuchert

Active Member
Licensed User
Longtime User
Thank you. Lately, I have been simply transferring files with the sd card itself in my computer as a drive, then replacing it in the device after completing. I don't have to remember the other nuances.
Jim
 
Upvote 0
Top