我想在可移动sd卡中解压缩.zip文件。我正在使用文件选择器选择.zip文件。选择文件后,我将得到如下路径:
“ content://com.android.externalstorage.documents/document/13E7-3B0A%3Azipfolder.zip” 然后选择文件夹以将文件解压缩到所选位置 它给出了这样的路径 “ content://com.android.externalstorage.documents/tree/13E7-3B0A%3ALOST.DIR”。 之后,我使用一种方法来解压缩.zip文件 System.IO.Compression.ZipFile.ExtractToDirectory(“ content://com.android.externalstorage.documents/document/13E7-3B0A%3Azipfolder.zip”, “ content://com.android.externalstorage.documents/tree/13E7-3B0A%3ALOST.DIR”);
但是它给出了下面提到的错误:
未处理的异常:System.UnauthorizedAccessException:访问 路径“ / content:”被拒绝。发生
我也尝试赋予运行时权限,但是仍然出现相同的错误。请帮助我,因为我花了太多时间
用于选择文件并获取路径的代码
private async void SelectZipFile(object sender, EventArgs e)
{
string[] allowedTypes = { "application/zip" };//i use allowedType in PickFile for selecting only .zip file
FileData file = await CrossFilePicker.Current.PickFile(allowedTypes);
if (file != null)
{
//string filename = file.FileName;
_zipFile = file.FilePath;//"content://com.android.externalstorage.documents/document/13E7-3B0A%3Azipfolder.zip"
txt1.Text = file.FileName;
//int indx=_zipFile.IndexOf(filename);
//string ss=_zipFile.Remove(indx);
//_zipFile = ss + "/" + filename;
}
}
解压缩文件的代码
public static void Unzip(String _location)
{
if (_location != null)
{
Label lbl1 = new Label();
lbl1.Text = "Extracting files.......";
System.IO.Compression.ZipFile.ExtractToDirectory(_zipFile, _location);
lbl1.IsVisible = false;
}
}
获取位置的代码
public void SelectDirectory()
{
try
{
activity = Xamarin.Forms.Forms.Context as MainActivity;
activity.Intent = new Intent();
activity.Intent.SetAction(Intent.ActionOpenDocumentTree);
activity.StartActivityForResult(activity.Intent, REQUEST_CODE_OPEN_DIRECTORY);
activity.ActivityResult += (object sender, ActivityResultEventArgs e) =>
{
FolderPath = e.Intent.Data;
//string location = FolderPath.ToString();
//string[] arr = FolderPath.LastPathSegment.Split(":");
//int indx = location.IndexOf(arr[1]);
//string ss = location.Remove(indx);
//location = ss + "/" + arr[1];
MainPage.Unzip(FolderPath.ToString());
};
}
catch(Exception ex)
{
string str = ex.ToString();
}
}
答案 0 :(得分:0)
您可以使用我创建的库,该库使这项工作变得如此轻松,可以使用android存储访问框架(SAF)应用于SD卡,USB和外部存储: https://github.com/madnik7/PortableStorage
private const int browseRequestCode = 100; //Just a unique number
// Select a folder by Intent
private void BrowseOnClick(object sender, EventArgs eventArgs)
{
SafStorageHelper.BrowserFolder(this, browseRequestCode);
}
// Access the folder via SafStorgeProvider
protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode == BROWSE_REQUEST_CODE && resultCode == Result.Ok)
{
var uri = SafStorageHelper.ResolveFromActivityResult(this, data);
var storage = SafStorgeProvider.CreateStorage(this, uri);
storage.CreateStorage("_PortableStorage.Test");
storage.WriteAllText("test.txt", "123");
}
}
答案 1 :(得分:0)
与内部SDcard文件夹不同,除专用于您自己的应用程序的文件夹(类似于以下形式)外,您无法访问可移动SD卡上的任何任意文件夹
/storage/XXXX-XXXX/Android/data/Your_app_bundle_id/files
这是C#运行时的限制,不是Android系统的限制。如果使用本机插件,您仍然可以访问可移动SD卡。