Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;  import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL;  import android.os.Environment; import android.util.Log;  public class Downloader {      public void DownloadFile(String fileURL, String fileName) {         try {             File root = Environment.getExternalStorageDirectory();             URL u = new URL(fileURL);             HttpURLConnection c = (HttpURLConnection) u.openConnection();             c.setRequestMethod("GET");             c.setDoOutput(true);             c.connect();             FileOutputStream f = new FileOutputStream(new File(root, fileName));              InputStream in = c.getInputStream();              byte[] buffer = new byte[1024];             int len1 = 0;             while ((len1 = in.read(buffer)) > 0) {                 f.write(buffer, 0, len1);             }             f.close();         } catch (Exception e) {             Log.d("Downloader", e.getMessage());         }      } } 

However, using Environment.getExternalStorageDirectory(); means that the file will always write to the root /mnt/sdcard. Is it possible to specify a certain folder to write the file to?

For example: /mnt/sdcard/myapp/downloads

like image 850
RailsSon Avatar asked Aug 23 '10 21:08

RailsSon


People also ask

How do I write to my external storage?

To read and write data to external storage, the app required WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE system permission. These permissions are added to the AndroidManifest. xml file. Add these permissions just after the package name.


2 Answers

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); ... 
like image 53
plugmind Avatar answered Oct 06 '22 00:10

plugmind


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(); 
like image 25
hcpl Avatar answered Oct 06 '22 00:10

hcpl