在SD卡上写入权限的权限

时间:2010-10-13 16:21:45

标签: android permissions sd-card

写入SD卡时遇到问题,以下是代码: (对不起代码的布局,只需复制它)

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

我还没有实现readFromFile()。 问题是root.canWrite()一直返回false。 这是清单文件:

<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" />

如果我去设置,我已经要求获得写入权限,并且在我的avd上 - &gt; SD卡和手机存储,它告诉我我在SD卡上有1 GB写入。请帮忙。

感谢:)

4 个答案:

答案 0 :(得分:6)

尝试写入之前,请尝试检查SD卡的状态。它可以用作共享驱动器,已损坏,已满,等等。可在此处找到状态列表:http://developer.android.com/reference/android/os/Environment.html

以下是获取状态的示例:

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

答案 1 :(得分:3)

您看到哪些结果是从writeToFile重新发送的? “文件未写”或“文件不能写”?

当我运行你的代码时,它会进入catch IOException块,结果是“file not written”。原因是fos定义不正确:

fos = new FileOutputStream( saveFileName );

应该是:

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

在我更改了这一行之后,我从writeToFile返回了“File written”结果。

答案 2 :(得分:0)

我使用以下代码将音频数据(成功)录制到我的Android SD卡。我的应用程序需要RECORD_AUDIO权限,但没有别的。

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");
}

在我看来,你错过了附加到你的getExternalStorageDirectory()调用的getAbsolutePath()调用(可能与Marc Bernstein的硬编码解决方案具有相同的结果)。

答案 3 :(得分:0)

我也应该包含以下链接(因为它是我获得写入SD卡的代码的地方......):

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