gpt4 book ai didi

file-io - Mono for Android 外部(SD 卡)文件 IO

转载 作者:行者123 更新时间:2023-12-01 22:27:22 27 4
gpt4 key购买 nike

我正在尝试创建我的第一个 Mono for Android 应用程序,并且在尝试读取或写入 SD 卡时遇到权限错误。因此,我已切换到 SanityTests,xamarin-monodroid-samples-d76baf3。

Example_WorkingWithAudio RecordAudio.cs 在内部存储上成功运行。它还在 list 中应用了 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”)

但是该行给出了异常(exception):

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

最佳答案

这就是我在 Android 版 Mono 中完成此操作的方法。

var path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;

path = Path.Combine(path, "testaudio.mp3");

关于file-io - Mono for Android 外部(SD 卡)文件 IO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13133032/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com