Realization of reading and writing SD card by Android
- 2021-07-10 20:49:49
- OfStack
Reading and writing SD cards is the most common operation in the process of developing Android applications. The following describes the read and write operation mode of SD card:
1. Get the root directory of the SD card
String sdCardRoot = Environment.getExternalStorageDirectory().getAbsolutePath();
2. Create a folder directory on an SD card
/**
* In SD Create a directory on the card
*/
public File createDirOnSDCard(String dir)
{
File dirFile = new File(sdCardRoot + File.separator + dir +File.separator);
Log.v("createDirOnSDCard", sdCardRoot + File.separator + dir +File.separator);
dirFile.mkdirs();
return dirFile;
}
3. Create a file on an SD card
/**
* In SD Create a file on the card
*/
public File createFileOnSDCard(String fileName, String dir) throws IOException
{
File file = new File(sdCardRoot + File.separator + dir + File.separator + fileName);
Log.v("createFileOnSDCard", sdCardRoot + File.separator + dir + File.separator + fileName);
file.createNewFile();
return file;
}
4. Determine whether the file exists in a directory of SD card
/**
* Judge SD Does the file exist on the card
*/
public boolean isFileExist(String fileName, String path)
{
File file = new File(sdCardRoot + path + File.separator + fileName);
return file.exists();
}
5. Write data to the SD card specified directory file
/* Write data to SD Card
*/
public File writeData2SDCard(String path, String fileName, InputStream data)
{
File file = null;
OutputStream output = null;
try {
createDirOnSDCard(path); // Create Directory
file = createFileOnSDCard(fileName, path); // Create a file
output = new FileOutputStream(file);
byte buffer[] = new byte[2*1024]; // Every time I write 2K Data
int temp;
while((temp = data.read(buffer)) != -1 )
{
output.write(buffer,0,temp);
}
output.flush();
} catch (Exception e) {
e.printStackTrace();
}
finally{
try {
output.close(); // Turn off the data flow operation
} catch (Exception e2) {
e2.printStackTrace();
}
}
return file;
}
Matters needing attention
To operate SD card, you must apply for permission:
<
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/
>
See https://www.ofstack.com/article/34296. htm for details
Note: Do not read directly is to prevent file operation on memory consumption, so the input stream read to the hard disk.
public String readFile(String fileName) throws Exception{
FileInputStream fis = context.openFileInput(fileName);
byte[] bytes = new byte[fis.available()];
fis.read(bytes);
fis.close();
return new String(bytes,"utf-8");
}
When the file is very large, byte [] will take up a lot of memory.
package cn.itcast.fileio.service;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.os.Environment;
public class SdCardService {
private Context ctx;
public SdCardService(Context ctx) {
this.ctx = ctx;
}
/**
* Write file into skcard
*/
public void writeToSdCard(String fileName, String cont) {
try {
// Determine if there is a mount sdcard
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// Get sdcar File directory
File dir = Environment.getExternalStorageDirectory();
File file = new File(dir, "itcast.txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write(cont.getBytes());
fos.close();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* Read SdCard Files in
*/
public String readSdCard(String fileName) {
try {
// Determine if there is a mount sdcard
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// Get sdcar File directory
File dir = Environment.getExternalStorageDirectory();
File file = new File(dir, "itcast.txt");
FileInputStream fis = new FileInputStream(file);
return readIs2String(fis);
}
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Read the input stream data into the output stream
*/
private OutputStream readIs2Os(InputStream is ,OutputStream os){
try {
byte[] bytes = new byte[1024];
int length = 0 ;
while((length = is.read(bytes)) != -1){
os.write(bytes, 0, length);
}
is.close();
os.close();
}
catch (IOException e) {
e.printStackTrace();
}
return os ;
}
/**
* Read the input stream data into the output stream
*/
public byte[] readIs2Bytes(InputStream is){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
readIs2Os(is,baos);
return baos.toByteArray() ;
}
public String readIs2String(InputStream is){
try {
return new String(readIs2Bytes(is),"utf-8");
}
catch (Exception e) {
e.printStackTrace();
}
return null ;
}
public String readIs2String(String fileName){
try {
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File dir = Environment.getExternalStorageDirectory();
File file = new File(dir,fileName);
FileInputStream is = new FileInputStream(file);
return readIs2String(is);
}
}
catch (Exception e) {
e.printStackTrace();
}
return null ;
}
}