我无法使用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");
}
}