2

我正在尝试创建我的第一个 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
4

4 回答 4

11

这就是我在 Mono for Android 中所做的。

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

path = Path.Combine(path, "testaudio.mp3");
于 2012-12-19T10:23:05.320 回答
4

我遇到了同样的异常,最后通过执行以下操作解决了它:

  1. 为模拟器创建一个 sdcard 文件。我用一个简单的教程写了一篇文章,描述了如何做到这一点。
  2. 使用下面的代码(C#),我能够将文件写入 sdcard:

    string pathToFile = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, _fileName);
    using(var fileStream = new FileStream(pathToFile, FileMode.Append, FileAccess.Write, FileShare.None))
    {
        fileStream.Write(Encoding.ASCII.GetBytes(message), 0, Encoding.ASCII.GetByteCount(message));
    }
    
于 2012-12-21T21:14:14.097 回答
0

如果您尝试使用 xamarin 表单从服务器下载或获取图像,您可以使用以下命令:

File.WriteAllBytes("myimage.png",
new WebClient().DownloadData(http://www.dotnetperls.com/net.png));
于 2014-09-05T21:32:43.103 回答
-1

试试这个路径:

String filePath = Environment.getExternalStorageDirectory() + "/testAudio.mp3";
File mFile = new File(filePath);
于 2012-10-30T06:02:22.347 回答