在安卓系统中写入SD卡的权限

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 卡上有 1GB 可以写入。请帮忙。

谢谢 :)

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

3

你从writeToFile返回的结果是“文件未写入”还是“文件无法写入”?

当我运行你的代码时,它跳到了带有“文件未写入”结果的catch IOException块中。原因是fos定义不正确:

fos = new FileOutputStream( saveFileName );

should be:

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

我修改了这一行后,从writeToFile返回了结果“文件已写入”。


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的硬编码解决方案具有相同的结果)。


0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接