【问题标题】:I get [ERROR] FATAL UNHANDLED EXCEPTION: Java.IO.FileNotFoundException: /mnt/media_rw/24A3-6FDF/hello.txt (Permission denied)我得到 [ERROR] FATAL UNHANDLED EXCEPTION: Java.IO.FileNotFoundException: /mnt/media_rw/24A3-6FDF/hello.txt(权限被拒绝)
【发布时间】:2018-06-18 16:43:31
【问题描述】:

我无法从使用 7.1.1 连接到 android 设备的 USB 驱动器中读取文件。 我的清单文件拥有所有权限。

我明白了

[错误] 致命的未处理异常:Java.IO.FileNotFoundException:/mnt/media_rw/24A3-6FDF/hello.txt(权限被拒绝)

这是我在主要活动中的代码 public void ReadFileFromStorage() {

 System.Diagnostics.Debug.WriteLine("inside ReadFileFromStorage");
 var mPermissionIntent = PendingIntent.GetBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
 UsbManager usbManager = (UsbManager)GetSystemService(Context.UsbService);
 var deviceList = usbManager.DeviceList;
        IEnumerable<UsbDevice> deviceIterator = deviceList.Values.AsEnumerable();
        if (deviceIterator.Count() > 0)
        {
            UsbDevice device = deviceIterator.ElementAt(0);
            usbManager.RequestPermission(device, mPermissionIntent);
            if (usbManager.HasPermission(device))
            {
                System.Diagnostics.Debug.WriteLine("HasPermission");
                UsbDeviceConnection connection = usbManager.OpenDevice(device);
                if( connection != null)
                {
                    System.Diagnostics.Debug.WriteLine(" connection is not null");

                    Java.IO.File dir = new Java.IO.File("/mnt/media_rw/C227-875D/");
                    if (dir.Exists())
                    {
                        fileList = dir.ListFiles();

                        Toast.MakeText(Android.App.Application.Context, "exception did not occur" + "directory exists", ToastLength.Long).Show();
                        System.Diagnostics.Debug.WriteLine("dir exists");

                        if (dir.IsDirectory)
                        {
                            System.Diagnostics.Debug.WriteLine(dir.Path);
                        }
                    }

                }

                var model = device.DeviceName;
                string inlne = null;
                BufferedReader reader = null;

                // read the contents of the file
                reader = new BufferedReader(new FileReader("/mnt/media_rw/C227-875D/test/hello.txt"));
                System.Diagnostics.Debug.WriteLine("no exception occured");

    }         

}

【问题讨论】:

  • 您需要运行时权限。 developer.android.com/training/permissions/requesting 示例包含在链接底部。 SO也有很多答案。
  • 我在运行时拥有所有权限。我很难包含我的代码
  • @NandiniNadig 你在这里仍然没有提供足够的信息。 什么您收到的错误是什么? 什么你试过没有用?
  • @Jonathan。这是我得到的错误:
  • [错误] 致命的未处理异常:Java.IO.FileNotFoundException:/mnt/media_rw/24A3-6FDF/hello.txt(权限被拒绝)01-11 03:04:55.401 E/mono-rt (5309):在(包装器动态方法)System.Object.28110580-fe63-4db0-96a8-22531e1d0520(intptr,intptr,intptr)01-11 03:04:55.401 E/mono-rt(5309):- - 托管 Java.IO.FileNotFoundException 堆栈跟踪结束 --- 01-11 03:04:55.401 E/mono-rt (5309): java.io.FileNotFoundException: /mnt/media_rw/24A3-6FDF/hello.txt (权限被拒绝)01-11 03:04:55.401 E/mono-rt (5309): at java.io.FileInputStream.open(Native Method)

标签: c# android xamarin.forms xamarin.android


【解决方案1】:

从 Android M+ 开始,您不仅需要在 Manifest 中请求权限,还需要在运行时请求权限。你可以用这个link来看看怎么做

我在您的代码中看到的第二个是您将设备用作 USB 主机。您需要为此调整清单。 小心使用 android docs。您还需要一个权限和uses-feature 标签

<uses-feature android:name="android.hardware.usb.host" />
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />

【讨论】:

  • 感谢您的回复。我确实在运行时请求了权限。它说我有权限,但给出了权限被拒绝的异常。不过,Mu 代码在 c# 上
  • USB 驱动器自动挂载在 /mnt/media_rw/(USB 驱动名称)上,即“/mnt/media_rw/C227-875D”
  • 抱歉,没有看到 xamarin 标签。您应该谷歌权限被拒绝异常或添加一些代码,以便人们可以更快地帮助您!并提及您确实请求许可
  • xmlns:android="schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.WebFormsClient" android:installLocation="auto">
  • 好吧,我编辑了我的答案,我不使用 xamarin,但我使用 android,直到这里没有 xamarin 专业人士,让我们看看我们是否可以通过文档解决这个问题
猜你喜欢
  • 1970-01-01
  • 2010-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-27
相关资源
最近更新 更多