[英]Permission to write on SD card android
我寫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可以寫入。 請幫忙。
謝謝:)
在嘗試寫入SD卡之前,請嘗試檢查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;
}
您從writeToFile返回哪個結果? “文件未寫”或“文件不能寫”?
當我運行你的代碼時,它會進入catch IOException塊,結果為“file not written”。 原因是fos定義不正確:
fos = new FileOutputStream( saveFileName );
應該:
fos = new FileOutputStream( root + "/" saveFileName );
在我更改了這一行之后,我從writeToFile返回了“File written”結果。
我使用以下代碼將音頻數據(成功)錄制到我的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的硬編碼解決方案具有相同的結果)。
我應該也包括以下鏈接(因為它是我獲得寫入SD卡的代碼的地方......):
http://emeadev.blogspot.com/2009/09/raw-audio-manipulation-in-android.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.