Skip to content Skip to sidebar Skip to footer

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; pri

Solution 1:

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;
    } elseif (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        mExternalStorageAvailable = true;
        mExternalStorageWriteable = false;
    } else {
        mExternalStorageAvailable = mExternalStorageWriteable = false;
    }

Solution 2:

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.

Solution 3:

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

Filefile=newFile (Environment.getExternalStorageDirectory().getAbsolutePath() + FILE_NAME);

if (file.exists())
    file.delete();                                                          //  Delete any previous recordingtry
{
    file.createNewFile();                                                   //  Create the new file
}
catch (IOException e)
{
    Log.e (TAG, "Failed to create " + file.toString());
}

try
{
    OutputStreamos=newFileOutputStream      (file);
    BufferedOutputStreambos=newBufferedOutputStream  (os, 8000);
    DataOutputStreamdos=newDataOutputStream      (bos);          //  Create a DataOutputStream to write the audio data to the file// Create an AudioRecord object to record the audiointbufferSize= AudioRecord.getMinBufferSize (frequency, channelConfig, Encoding);
    AudioRecordaudioRecord=newAudioRecord (MediaRecorder.AudioSource.MIC, frequency, channelConfig, Encoding, bufferSize);

    short[] buffer = newshort[bufferSize];                                 //  Using "short", because we're using "ENCODING_PCM_16BIT"    
    audioRecord.startRecording();

    while (isRecording)
    {
        intbufferReadResult= audioRecord.read (buffer, 0, bufferSize);
        for (inti=0; i < bufferReadResult; i++)
            dos.writeShort (buffer[i]);
    }

    audioRecord.stop();
    dos.close();
}
catch (Exception t)
{
    Log.e (TAG, "Recording Failed");
}

It would appear to me that you're missing the getAbsolutePath() call appended to your getExternalStorageDirectory() call (which may have the same result as Marc Bernstein's hard coded solution).

Solution 4:

I should have included the following link as well (since it's where I got the code that writes to the SD Card...):

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

Post a Comment for "Permission To Write On Sd Card Android"