SoFunction
Updated on 2025-04-10

Android development SD card file operation analysis

This article describes the operation of SD card file for Android development. Share it for your reference, as follows:

The previous article wrote about directly operating the program that comes with the mobile phone's own memory. This time, I followed the last article agreement to operate the file of the SD card. Unlike the built-in storage, the user authorization is required to use the SD card.

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

Remember not to write in reverse, otherwise you won't see the results

Write a method to save the file

public void saveToSD(String filename,String content) throws Exception{
    //getExternalStorageDirectory() can obtain the SD card path    File f=new File((),filename);
    FileOutputStream out2=new FileOutputStream(f);
    (());
    ();
}

Finally, you can use this method in the control layer. You need to make a judgment on the state of the SD card. You can use it if you obtain the state.(), if it is available, the file can be saved. Otherwise, it will prompt "The SD card does not exist or is not available"

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class FileActivity extends Activity {
  private FileService service;
  public void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView();
    service=new FileService(this);
    Button button=(Button)findViewById();
    (new () {
      public void onClick(View v) {
        EditText filename=(EditText)findViewById();
        EditText content=(EditText)findViewById();
        try {
          if(().equals(Environment.MEDIA_MOUNTED)){
            (().toString(), ().toString());
            (, , 1).show();
          }else{
            (, , 1).show();
          }
          //(().toString(), ().toString());
        } catch (Exception e) {
          (, , 1).show();
          ("FileActivity", ());
        }
      }
    });
  }
}

Post it below

&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;resources&gt;
  &lt;string name="hello"&gt;Hello World, FileActivity!&lt;/string&gt;
  &lt;string name="app_name"&gt;Reading of files&lt;/string&gt;
  &lt;string name="filename"&gt;Enter a file name&lt;/string&gt;
  &lt;string name="content"&gt;Enter the file contents&lt;/string&gt;
  &lt;string name="button"&gt;save&lt;/string&gt;
  &lt;string name="success"&gt;文件save成功&lt;/string&gt;
  &lt;string name="failure"&gt;文件save失败&lt;/string&gt;
  &lt;string name="sd"&gt;sdThe card does not exist or is not available&lt;/string&gt;
&lt;/resources&gt;

Here you can operate the SD card, and there are fewer things this time.

For more information about Android related content, please check out the topic of this site:A summary of SD card operation methods for Android programming and development》、《Android file operation skills summary》、《Android database operation skills summary》、《Android programming activity operation skills summary》、《Android development introduction and advanced tutorial》、《Android resource operation skills summary》、《Android View View Tips Summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.