How do you write to a folder on an SD card in Android?

后端 未结 4 1335
深忆病人
深忆病人 2020-11-22 07:50

I am using the following code to download a file from my server then write it to the root directory of the SD card, it all works fine:

package com.downloader;         


        
相关标签:
4条回答
  • 2020-11-22 08:30

    Found the answer here - http://mytechead.wordpress.com/2014/01/30/android-create-a-file-and-write-to-external-storage/

    It says,

    /**
    
    * Method to check if user has permissions to write on external storage or not
    
    */
    
    public static boolean canWriteOnExternalStorage() {
       // get the state of your external storage
       String state = Environment.getExternalStorageState();
       if (Environment.MEDIA_MOUNTED.equals(state)) {
        // if storage is mounted return true
          Log.v("sTag", "Yes, can write to external storage.");
          return true;
       }
       return false;
    }
    

    and then let’s use this code to actually write to the external storage:

    // get the path to sdcard
    File sdcard = Environment.getExternalStorageDirectory();
    // to this path add a new directory path
    File dir = new File(sdcard.getAbsolutePath() + "/your-dir-name/");
    // create this directory if not already created
    dir.mkdir();
    // create the file in which we will write the contents
    File file = new File(dir, "My-File-Name.txt");
    FileOutputStream os = outStream = new FileOutputStream(file);
    String data = "This is the content of my file";
    os.write(data.getBytes());
    os.close();
    

    And this is it. If now you visit your /sdcard/your-dir-name/ folder you will see a file named - My-File-Name.txt with the content as specified in the code.

    PS:- You need the following permission -

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
    0 讨论(0)
  • 2020-11-22 08:32

    In order to download a file to Download or Music Folder In SDCard

    File downlodDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);// or DIRECTORY_PICTURES
    

    And dont forget to add these permission in manifest

     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    
    0 讨论(0)
  • 2020-11-22 08:43

    Add Permission to Android Manifest

    Add this WRITE_EXTERNAL_STORAGE permission to your applications manifest.

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="your.company.package"
        android:versionCode="1"
        android:versionName="0.1">
        <application android:icon="@drawable/icon" android:label="@string/app_name">
            <!-- ... -->
        </application>
        <uses-sdk android:minSdkVersion="7" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    </manifest> 
    

    Check availability of external storage

    You should always check for availability first. A snippet from the official android documentation on external storage.

    boolean mExternalStorageAvailable = false;
    boolean mExternalStorageWriteable = false;
    String state = Environment.getExternalStorageState();
    
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // We can read and write the media
        mExternalStorageAvailable = mExternalStorageWriteable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // We can only read the media
        mExternalStorageAvailable = true;
        mExternalStorageWriteable = false;
    } else {
        // Something else is wrong. It may be one of many other states, but all we need
        //  to know is we can neither read nor write
        mExternalStorageAvailable = mExternalStorageWriteable = false;
    }
    

    Use a Filewriter

    At last but not least forget about the FileOutputStream and use a FileWriter instead. More information on that class form the FileWriter javadoc. You'll might want to add some more error handling here to inform the user.

    // get external storage file reference
    FileWriter writer = new FileWriter(getExternalStorageDirectory()); 
    // Writes the content to the file
    writer.write("This\n is\n an\n example\n"); 
    writer.flush();
    writer.close();
    
    0 讨论(0)
  • 2020-11-22 08:48
    File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File (sdCard.getAbsolutePath() + "/dir1/dir2");
    dir.mkdirs();
    File file = new File(dir, "filename");
    
    FileOutputStream f = new FileOutputStream(file);
    ...
    
    0 讨论(0)
提交回复
热议问题