Android 中的“找不到路径的一部分”错误

在我们开始之前,我知道很多人提出了类似的问题,但我无法解决我的问题。


我尝试将位图保存到 Android 设备的图库中,但我无法设法到达该特定路径。


这是代码:


string root = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;

Java.IO.File myDir = new Java.IO.File(root + "/Screenshots");

myDir.Mkdirs();


string fname = "test_picture.jpg";


Java.IO.File file = new Java.IO.File(myDir, fname);

if (file.Exists())

    file.Delete();

try

{

    FileStream outStream = new FileStream(file.Path, FileMode.OpenOrCreate); //Seems like the problem is here

    finalBitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, outStream);

    outStream.Flush();

    outStream.Close();

}

catch (Exception e)

{

    Toast.MakeText(Activity, e.ToString(), ToastLength.Long).Show();

}

我收到此错误:


System.IO.DirectoryNotFoundException:找不到路径“/storage/emulated/0/Screenshots/test_picture.jpg”的一部分


路径有什么问题?错误的根源是什么?


PS:我已经给了所有需要的权限。


青春有我
浏览 117回答 1
1回答

FFIVE

我收到下一个错误:System.IO.DirectoryNotFoundException:找不到路径“/storage/emulated/0/Screenshots/test_picture.jpg”的一部分。测试您的代码后,此错误发生在以下代码中:FileStream outStream = new FileStream(file.Path, FileMode.OpenOrCreate);如果使用它string root = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;,您应该注意到这是一个公共外部文件。/存储/模拟/0/文档从文档中可以看出,Android 应用程序必须先获得许可才能读取或写入任何公共文件。所以在这里你需要添加权限AndroidManifest.xml:<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />而从android 6.0开始,还需要添加runtimer权限。这里,建议可以安装Plugin.Permissions NuGet包给项目申请权限。如下示例:创建时():Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this,savedInstanceState);按钮点击方法():try{&nbsp; &nbsp; var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);&nbsp; &nbsp; if (status != PermissionStatus.Granted)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Storage))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //await DisplayAlert("Need location", "Gunna need that location", "OK");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("OK");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Storage);&nbsp; &nbsp; &nbsp; &nbsp; //Best practice to always check that the key exists&nbsp; &nbsp; &nbsp; &nbsp; if (results.ContainsKey(Permission.Storage))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; status = results[Permission.Storage];&nbsp; &nbsp; }&nbsp; &nbsp; if (status == PermissionStatus.Granted)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; string root = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;&nbsp; &nbsp; &nbsp; &nbsp; Java.IO.File myDir = new Java.IO.File(root + "/Screenshots");&nbsp; &nbsp; &nbsp; &nbsp; myDir.Mkdirs();&nbsp; &nbsp; &nbsp; &nbsp; string fname = "test_picture.jpg";&nbsp; &nbsp; &nbsp; &nbsp; Java.IO.File file = new Java.IO.File(myDir, fname);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("------root-------" + file.Path);&nbsp; &nbsp; &nbsp; &nbsp; FileStream outStream = new FileStream(file.Path, FileMode.OpenOrCreate);&nbsp; &nbsp; &nbsp; &nbsp; ....&nbsp; &nbsp; }&nbsp; &nbsp; else if (status != PermissionStatus.Unknown)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("OK");&nbsp; &nbsp; &nbsp; &nbsp; //await DisplayAlert("Location Denied", "Can not continue, try again.", "OK");&nbsp; &nbsp; }}catch (Exception ex){&nbsp; &nbsp; Console.WriteLine("" + ex);}其他解决方案:如果不使用公共外部文件,您可以尝试使用私有外部文件/storage/emulated/0/Android/data/com.companyname.app/files/这不需要运行时权限。但是AndroidManifest.xml也需要静态权限。string root = Application.Context.GetExternalFilesDir("DirectoryPictures").ToString();
打开App,查看更多内容
随时随地看视频慕课网APP