【发布时间】: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