gpt4 book ai didi

java - Android 中的 “Could not find a part of the path” 错误

转载 作者:行者123 更新时间:2023-12-02 09:58:39 24 4
gpt4 key购买 nike

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

我尝试将位图保存到 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: Could not find a part of the path "/storage/emulated/0/Screenshots/test_picture.jpg"

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

P.S:我已经授予了所有需要的权限。

最佳答案

I get the next error: System.IO.DirectoryNotFoundException: Could not find a part of the path "/storage/emulated/0/Screenshots/test_picture.jpg".

测试您的代码后,以下代码中发生此错误:

FileStream outStream = new FileStream(file.Path, FileMode.OpenOrCreate);

如果使用此 string root = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath; ,您应该注意到这是一个 Public external files .

/storage/emulated/0/Documents

根据文档,Android 应用程序必须获得权限才能读取或写入任何公共(public)文件。因此,您需要在 AndroidManifest.xml 中添加权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

并且从android 6.0开始,还需要添加运行时权限。这里,建议可以为项目安装Plugin.Permissions NuGet包来请求权限。如下是一个示例:

OnCreate():

Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this,savedInstanceState);

按钮点击方法():

try
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);
if (status != PermissionStatus.Granted)
{
if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Storage))
{
//await DisplayAlert("Need location", "Gunna need that location", "OK");
Console.WriteLine("OK");
}

var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Storage);
//Best practice to always check that the key exists
if (results.ContainsKey(Permission.Storage))
status = results[Permission.Storage];
}

if (status == PermissionStatus.Granted)
{
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);
Console.WriteLine("------root-------" + file.Path);

FileStream outStream = new FileStream(file.Path, FileMode.OpenOrCreate);
....
}
else if (status != PermissionStatus.Unknown)
{
Console.WriteLine("OK");
//await DisplayAlert("Location Denied", "Can not continue, try again.", "OK");
}
}
catch (Exception ex)
{
Console.WriteLine("" + ex);
}

其他解决方案:

如果不使用公共(public)外部文件,您可以尝试使用 Private external files

/storage/emulated/0/Android/data/com.companyname.app/files/

这不需要运行时权限。但是,还需要 AndroidManifest.xml 中的静态权限。

string root = Application.Context.GetExternalFilesDir("DirectoryPictures").ToString();

关于java - Android 中的 “Could not find a part of the path” 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55795915/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com