我正在尝试创建我的第一个 Mono for Android 应用程序,并且在尝试读取或写入 sdcard 时出现权限错误。所以我切换到了 SanityTests,xamarin-monodroid-samples-d76baf3。
Example_WorkingWithAudio RecordAudio.cs 在内部存储上成功运行。它还具有在清单中应用的 WRITE_EXTERNAL_STORAGE 权限;所以我然后取消注释这些行:
Java.IO.File sdDir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryMusic);
filePath = sdDir + "/" + "testAudio.mp3";
因此代码现在变为:
Java.IO.File sdDir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryMusic);
filePath = sdDir + "/" + "testAudio.mp3";
Java.IO.File myFile = new Java.IO.File(filePath);
myFile.CreateNewFile();
我在运行 Android 2.3.4 的索尼爱立信 Xperia Mini Pro 上执行此操作。
当执行到达 CreateNewFile() 行时,filePath 的值为:“/mnt/sdcard/Music/testAudio.mp3”。
然后抛出此错误:
Java.IO.IOException: Exception of type 'Java.IO.IOException' was thrown.
at Android.Runtime.JNIEnv.CallBooleanMethod (IntPtr jobject, IntPtr jmethod) [0x00023] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.6-
branch/632e6ddf/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:156
at Java.IO.File.CreateNewFile () [0x0003e] in /Users/builder/data/lanes/monodroid-mac-monodroid-4.2.6-
branch/632e6ddf/source/monodroid/src/Mono.Android/platforms/android-8/src/generated/Java.IO.File.cs:771
at Example_WorkingWithAudio.RecordAudio.StartRecorder () [0x00044] in c:\Users\Matt\Downloads\xamarin-monodroid-samples-d76baf3\xamarin-monodroid-samples-
d76baf3\Example_WorkingWithAudio\Example_WorkingWithAudio\RecordAudio.cs:35
--- End of managed exception stack trace ---
java.io.IOException: Permission denied
at java.io.File.createNewFileImpl(Native Method)
at java.io.File.createNewFile(File.java:1257)
at mono.android.view.View_OnClickListenerImplementor.n_onClick(Native Method)
at mono.android.view.View_OnClickListenerImplementor.onClick(View_OnClickListenerImplementor.java:29)
at android.view.View.performClick(View.java:2552)
at android.view.View$PerformClick.run(View.java:9229)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3701)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
at dalvik.system.NativeStart.main(Native Method)
问题是什么?
仅供参考,我也尝试使用 .Net 类:
FileStream fileStream = File.Create(filePath);
(其中 filePath 仍然是“/mnt/sdcard/Music/testAudio.mp3”)
但是该行给出了例外:
System.IO.DirectoryNotFoundException: Could not find a part of the path "/mnt/sdcard/Music/testAudio.mp3".
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in <filename unknown>:0
at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) [0x00000] in <filename unknown>:0
at System.IO.File.Create (System.String path, Int32 bufferSize) [0x00000] in <filename unknown>:0
at System.IO.File.Create (System.String path) [0x00000] in <filename unknown>:0
at Example_WorkingWithAudio.RecordAudio.StartRecorder () [0x0001d] in c:\Users\Matt\Downloads\xamarin-monodroid-samples-d76baf3\xamarin-monodroid-samples-d76baf3\Example_WorkingWithAudio\Example_WorkingWithAudio\RecordAudio.cs:33