4

我在写入 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 上,如果我进入设置 - > SD 卡和手机存储,它告诉我 SD 卡上有 1 GB 的空间可以写入。请帮忙。

谢谢:)

4

4 回答 4

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;
    }
于 2010-10-13T16:32:58.330 回答
3

您看到从 writeToFile 返回的结果是什么?“文件未写入”或“文件无法写入”?

当我运行您的代码时,它会以“文件未写入”的结果进入 catch IOException 块。原因是 fos 定义不正确:

fos = new FileOutputStream( saveFileName );

应该:

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

更改此行后,我得到了从 writeToFile 返回的结果“已写入文件”。

于 2010-10-13T18:25:38.847 回答
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 的硬编码解决方案相同)。

于 2010-10-13T19:26:46.953 回答
0

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

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

于 2010-10-13T19:32:35.940 回答