Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permission to write on SD card android

I have a problem writing to the SD card, here is the code: (Sorry about the layout of the code, just copy pased it )

public class SaveAndReadManager {

 private String result;
 private String saveFileName = "eventlist_savefile";

 public String writeToFile( ArrayList<Event> arrlEvents ){
  FileOutputStream fos = null;
  ObjectOutputStream out = null;

  try{
   File root = Environment.getExternalStorageDirectory();

   if( root.canWrite() ){
    fos = new FileOutputStream( saveFileName );
    out = new ObjectOutputStream( fos );
    out.writeObject( arrlEvents );

    result = "File written";

    out.close();
   }else{
    result = "file cant write";
   }
  }catch( IOException e ){
   e.printStackTrace();
   result = "file not written";
  }

  return result;
 }

 public boolean readFromFile(){
  return false;
 }
}

I have not implemented the readFromFile() yet. The problem is that root.canWrite() returns false all the time. Here is the manifest file:

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

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".InfoScreen"
           android:label="@string/app_name">
      <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    <activity android:name=".EventCalendar"
              android:label="@string/app_name" />

    <activity android:name=".MakeEvent"
              android:label="@string/app_name" />

    <activity android:name=".ViewEvent"
              android:label="@string/app_name" />

</application>
<uses-sdk android:minSdkVersion="8" />

I've asked for the permission to write, and on my avd, if i go to settings -> SD card and phone storage, it tells me i have 1 gb on the sd card to write on. Please help.

Thanks:)

like image 654
lands Avatar asked Jan 22 '23 07:01

lands


2 Answers

Try checking the state of the SD card before you attempt to write to it. It may be used as a shared drive, corrupted, full, etc. A list of states can be found here: http://developer.android.com/reference/android/os/Environment.html

Here's an example of getting the states:

String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        mExternalStorageAvailable = mExternalStorageWriteable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        mExternalStorageAvailable = true;
        mExternalStorageWriteable = false;
    } else {
        mExternalStorageAvailable = mExternalStorageWriteable = false;
    }
like image 173
Josh Clemm Avatar answered Jan 23 '23 21:01

Josh Clemm


Which result are you seeing retunred from writeToFile? "file not written" or "file cant write"?

When I ran your code it dropped into the catch IOException block with the result of "file not written". The reason for this was that fos was defined incorrectly:

fos = new FileOutputStream( saveFileName );

should be:

fos = new FileOutputStream( root + "/" saveFileName );

After I changed this line, I got the result "File written" returned from writeToFile.

like image 42
Marc Bernstein Avatar answered Jan 23 '23 19:01

Marc Bernstein