androidandroid-memory

read and write files to removable sd card


How to read and write files to removable sd card in Android?

I want to store Android Id in Text file. The text file should be created on external sdcard.

Code:

PackageManager m = getPackageManager();
String s = getPackageName();
PackageInfo p = m.getPackageInfo(s, 0);
s = p.applicationInfo.dataDir;
File myFile = new File(s + "/MyDoople.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(TxtS.getText());
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),"Text Updated",Toast.LENGTH_SHORT).show();

The Second is

 File sdCard = new File("file:///mnt/external_sd/");
 File myFile = new File(sdCard, "test.txt");
 FileWriter writer = new FileWriter(myFile); 
 writer.append(TESTSTRING);
 writer.flush(); 
 writer.close();

Solution

  • My question is solved. I directly use the path "/mnt/external_sd/.. Path of File.".

    But it was only work with my Device