Android programming on SD card to do file read and write operation example details

  • 2020-11-26 18:59:28
  • OfStack

This article illustrates an Android programming method for reading and writing files on SD CARDS. To share for your reference, the details are as follows:

A lot of knowledge can only be used when you really understand and master. 1 to 3. Do you think there is a difference between the file operation in Java and the file operation in android system SD card? Obviously, there is no essential difference. If we barely say there is, it is not enough, but we should pay attention to the following points in the practical application, otherwise the problem will get stuck to you.

1. First, you need to add permission to operate android system SD card file:

android system will not let foreign programs arbitrarily move their own memory, if you do not have a license, I am sorry, do not allow you to move my site, in my site to listen to me. Add SD card access to AndroidManifest.xml configuration file.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

2. Get the absolute path of the file:

There are two ways to do it

Method 1: Use the /mnt/sdcard/+ filename directly
Such as: final String FILE_NAME = "/ mnt/sdcard / 00000. H264";

Method 2: Get the path to SD via the system-provided method, followed by the file name. This method is a bit verbose, temporarily did not feel its beauty, but here or listed, for future reference. See the following example.

3. The operation after obtaining the file path cannot skip the use of FileInputStream, FileOutputStream, FileReader and FileWriter4 classes. For details, please refer to Java Programming File Read-write above.

The following is a simple application code to write the data to the SD Cavendish:

Method 1 Using code:


public class FileTestActivity extends Activity
{
final String FILE_NAME = "/mnt/sdcard/007.test";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.button1);
final byte[] buf = { 0 };
btn.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
try
{
FileOutputStream fout = new FileOutputStream(FILE_NAME,
true);
BufferedOutputStream bout = new BufferedOutputStream(fout);
bout.write(buf);
bout.flush();
bout.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
});
}
}

Method 2 USES the example code


public class FileTestActivity extends Activity
{
final String FILE_NAME = "/007.test";
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button) findViewById(R.id.button1);
final byte[] buf = { 0 };
btn.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED))
{
File sdDir = Environment.getExternalStorageDirectory();
System.out.println(sdDir);
FileOutputStream fout;
try
{
System.out.println(sdDir.getCanonicalPath() + FILE_NAME);
fout = new FileOutputStream(sdDir.getCanonicalPath()
+ FILE_NAME, true);
BufferedOutputStream bout = new BufferedOutputStream(
fout);
bout.write(buf);
bout.flush();
bout.close();
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
});
}
}

I hope this article has been helpful to you in Android programming.


Related articles: