Recent

Author Topic: read/write text to sdcard  (Read 4635 times)

Gerbert

  • New Member
  • *
  • Posts: 11
read/write text to sdcard
« on: April 30, 2012, 08:58:45 am »
Is it possible to read/write a string to a sdcard from a LCL android application?
Do you have a example how to do this?
Even better is it to work with xml. Is this possible to?

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: read/write text to sdcard
« Reply #1 on: April 30, 2012, 09:26:35 am »
The sdcard is a filesystem, not a database, so you cannot write a string there. You can write a file which constains a string.

To do that you can use any of the Pascal file read/write operations. For example use TStringList and TStringList.SaveToFile()... I think that the path to the sdcard is /sdcard/ or something like that.

Gerbert

  • New Member
  • *
  • Posts: 11
Re: read/write text to sdcard
« Reply #2 on: May 03, 2012, 07:34:57 am »
Thanks felipemdc
I have implemented the following (based on your great example):

procedure TformLocatie.btnWriteDeviceInfoClick(Sender: TObject);
var
  slInfo:TStringList;
begin
  slInfo:=TStringList.Create;
  slInfo.Add('Device.Manufacturer='+Device.Manufacturer);
  slInfo.Add('Device.Model='+Device.Model);
  slInfo.SaveToFile('/sdcard/test_gerbert.txt');
end;

but when this code is executed the application crashes.
I have set the following line in the androidmanifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

(I use lazarus on openSuse 12.1)

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: read/write text to sdcard
« Reply #3 on: May 03, 2012, 07:46:51 am »
but when this code is executed the application crashes.

Every time this happens you need to look at what "adb logcat" says. Read the log and see what it says and also post it here.

Also use "adb shell" to navigate and see if /sdcard/ is the correct location.

Gerbert

  • New Member
  • *
  • Posts: 11
Re: read/write text to sdcard
« Reply #4 on: May 03, 2012, 10:26:14 am »
It seems my sdcard was not mounted after I disconnected the usb connection.
After a reboot my device the code is executed successfully.
Thanks.

 

TinyPortal © 2005-2018