Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use SD Card in Android Code

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about how Android uses SD cards in the code, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

1 check if SD is available

There are two ways, one is to write a listening function to listen

Private final BroadcastReceiver broadcastRec = new BroadcastReceiver () {@ Override public void onReceive (Context context Intent intent) {if (intent.getAction (). Equals ("android.intent.action.MEDIA_MOUNTED")) {/ / SD card has been successfully mounted / / there is a SD card} else if (intent.getAction (). Equals ("android.intent.action.MEDIA_REMOVED") | | intent.getAction (). Equals ("android.intent.action.ACTION_MEDIA_UNMOUNTED") | | intent.getAction (). Equals ("android.intent.action.ACTION_MEDIA_BAD_") REMOVAL ") {/ / various unmounted states / / no SD card}

The other, in my opinion, is simpler:

File f = new File ("/ sdcard/"); f.exists ()

2 read and write the data on the card

Before we get started, let's take a look at what classes / interfaces Android has prepared for us to read and write to the SD card.

API: FileFilter

Classes: File, FileInputStream, FileOutputStream

Let's give a brief introduction to the above in turn, and at the same time, we will also give some code to explain how each class uses

3 File

An abstract description class of a file system that can use relative and absolute paths. This class provides several methods for querying / modifying file information

File file = new File ("/ sdcard/a.txt")

Determine whether the file exists:

File.exists ()

Is "file" a directory or a file?

File.isDirectory ()

If the file is a directory, how do you list the subdirectories?

File [] filefiles = file.listFiles ()

For a file, if you get some information about the file

File.length (); / / the length of the file file.canRead (); / / can the file be read, file.canWrite (); / / can the file be written, file.delete (); / / delete the file

Next, give some snippet code to show how to use the File class

/ / create a new file File file = new File ("/ sdcard/a.txt"); if (! file.exists ()) {try {file.createNewFile ();} catch (IOException e) {e.printStackTrace ();}} / / create a new folder File file = new File ("/ sdcard/a/b/c"); if (! file.exists ()) {try {file.mkdirs ();}

Note the use of mkdirs () and mkdir (). Mkdirs () means that if necessary, the upper directory will be created first, as in the above code, if directory a does not exist in the SD card root directory, then using mkdir () will throw an exception.

4 FileFilter

We know that listFiles () lists the files in the current directory, but another problem arises: what if the files in the current directory are cluttered and we don't want to list all the files? FileFilter provides a solution. All we need to do is implement the public abstract boolean accept (File pathname) interface.

FileFilter filter = new FileFilter () {public boolean accept (File file) {if (file.isFile () & & file.getAbsolutePath () .toLowerCase () .endsWith (".txt")) {return true;} return false;}}; File [] filefiles = file.listFiles (filter)

The above code shows how to list only the TXT files in the current directory.

5 FileInputStream

We need to operate not only on the file, but also on the contents of the file, and then FileInputStream comes on stage.

FileInputStream inherits from InputStream, but FileInputStream uses buffering to improve performance when data is read in frequently.

It is also easy to use FileInputStream.

FileInputStream fis = new FileInputStream ("/ sdcard/a.txt"); byte [] abytes = new byte [1024] int len = fis.read (abytes)

In the above code, we open the a.txt file under the SD card root directory, and at the same time, read 1K of data, it should be noted that read does not guarantee

The certificate must read the expected length of data, and we need to check the length of the read data.

6 FileOutputStream

The usage of FileOutputStream is similar to that of OutputStream. Give me a code: FileOutputStream fos = new FileOutputStream ("/ sdcard/a.txt"); fos.write ("Hello World!" .getBytes ()); fos.flush ()

The above code writes Hello World characters to a.txt, and note that FileOutputStream also uses buffering, so the data is not immediately written to the file, and the real writeback occurs when the system thinks it needs to be written back, so if you want the data to be written back immediately, you need to use the flush () method.

Maybe you will ask, I don't want to erase the original contents of the file, why? This is simple, too.

FileOutputStream fos = new FileOutputStream ("/ sdcard/a.txt", true)

True means to append data to the end of the file

The above is how Android uses the SD card in the code. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report