Permission to write on Android SD card

I have a problem writing to an SD card, here is the code: (Sorry for the code layout, just copy 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 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 asked for permission to write, and on my avd, if I go to settings โ†’ SD card and phone, I will say that I have 1 gb on the SD card for recording. Please, help.

Thanks:)

+4
source share
4 answers

What result do you see retunred from writeToFile? "file not written" or "file can not write"?

When I ran your code, it fell into an IOException catch block with the result โ€œfile not writtenโ€. The reason for this was that fos was not defined correctly:

 fos = new FileOutputStream( saveFileName ); 

it should be:

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

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

+3
source

Try checking the status of the SD card before attempting to record it. It can be used as a shared disk, damaged, full, etc. A list of states can be found here: http://developer.android.com/reference/android/os/Environment.html

Here is an example of getting 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; } 
+6
source

I use the following code to record audio data (successfully) to my Android SD card. My application requires RECORD_AUDIO permission, but nothing else.

 File file = new File (Environment.getExternalStorageDirectory().getAbsolutePath() + FILE_NAME); if (file.exists()) file.delete(); // Delete any previous recording try { file.createNewFile(); // Create the new file } catch (IOException e) { Log.e (TAG, "Failed to create " + file.toString()); } try { OutputStream os = new FileOutputStream (file); BufferedOutputStream bos = new BufferedOutputStream (os, 8000); DataOutputStream dos = new DataOutputStream (bos); // Create a DataOutputStream to write the audio data to the file // Create an AudioRecord object to record the audio int bufferSize = AudioRecord.getMinBufferSize (frequency, channelConfig, Encoding); AudioRecord audioRecord = new AudioRecord (MediaRecorder.AudioSource.MIC, frequency, channelConfig, Encoding, bufferSize); short[] buffer = new short[bufferSize]; // Using "short", because we're using "ENCODING_PCM_16BIT" audioRecord.startRecording(); while (isRecording) { int bufferReadResult = audioRecord.read (buffer, 0, bufferSize); for (int i = 0; i < bufferReadResult; i++) dos.writeShort (buffer[i]); } audioRecord.stop(); dos.close(); } catch (Exception t) { Log.e (TAG, "Recording Failed"); } 

It seems to me that you miss the call to getAbsolutePath () attached to the call to getExternalStorageDirectory () (which may have the same result as Marc Bernstein's hard coded solution).

0
source

I also had to include the following link (since where I got the code that is written to the SD card ...):

http://emeadev.blogspot.com/2009/09/raw-audio-manipulation-in-android.html

0
source

All Articles