Einzelnen Beitrag anzeigen

Benutzerbild von KodeZwerg
KodeZwerg

Registriert seit: 1. Feb 2018
3.685 Beiträge
 
Delphi 11 Alexandria
 
#11

AW: Android SD-Kartenzugriff

  Alt 26. Mai 2018, 09:26
Das gehört zu meinem Helper Functions Link.
Zitat:
Apparently the most robust way to get access to the external storage location is through these two functions GetSysSecondaryStorage and GetSysExternalStorage.
Hier ist eine Diskussion über Permissions.
Hier in der DP gibts auch Infos zu diesem Thema!

Saving data to a file in your Android application geht da anders vor.

Zum Schreiben:
Delphi-Quellcode:
try {
  // Creates a file in the primary external storage space of the
  // current application.
  // If the file does not exists, it is created.
  File testFile = new File(this.getExternalFilesDir(null), "TestFile.txt");
  if (!testFile.exists())
      testFile.createNewFile();

  // Adds a line to the file
  BufferedWriter writer = new BufferedWriter(new FileWriter(testFile, true /*append*/));
  writer.write("This is a test file.");
  writer.close();
  // Refresh the data so it can seen when the device is plugged in a
  // computer. You may have to unplug and replug the device to see the
  // latest changes. This is not necessary if the user should not modify
  // the files.
  MediaScannerConnection.scanFile(this,
                                   new String[]{testFile.toString()}
,
                                   null,
                                   null);
} catch (IOException e) {
  Log.e("ReadWriteFile", "Unable to write to the TestFile.txt file.");
}
zum Lesen:
Delphi-Quellcode:
String textFromFile = "";
// Gets the file from the primary external storage space of the
// current application.
File testFile = new File(this.getExternalFilesDir(null), "TestFile.txt");
if (testFile != null) {
  StringBuilder stringBuilder = new StringBuilder();
  // Reads the data from the file
  BufferedReader reader = null;
  try {
      reader = new BufferedReader(new FileReader(testFile));
      String line;

      while ((line = reader.readLine()) != null) {
        textFromFile += line.toString();
        textFromFile += "\n";
      }

      reader.close();
   } catch (Exception e) {
      Log.e("ReadWriteFile", "Unable to read the TestFile.txt file.");
   }

}
Source Code Link davon


Klappt es damit?
Gruß vom KodeZwerg
  Mit Zitat antworten Zitat